Introduction
In this project, you will learn how to create a Go program that outputs the current day of the week and determines if it is Wednesday. This project is designed to help you get familiar with the basic syntax and structure of Go programming, as well as working with the built-in time package.
👀 Preview
## Example 1
Today is Sunday
## Example 2
The day is Wednesday
🎯 Tasks
In this project, you will learn:
- How to create a new Go file and set up the basic structure of a Go program
- How to use the
time.Now()function to get the current time - How to use the
Weekday()method to get the current day of the week - How to output information to the console using
fmt.Println() - How to check if the current day is Wednesday
🏆 Achievements
After completing this project, you will be able to:
- Create a simple Go program that outputs the current day of the week
- Determine if the current day is Wednesday
- Understand the basic syntax and structure of Go programming
- Gain experience working with the built-in
timepackage in Go
Create the today.go File
In this step, you will create a new Go file named today.go in the project's main directory ~/project. Follow the steps below to complete this step:
- Open your preferred text editor or IDE.
- Create a new file named
today.goin the~/projectdirectory. - Copy the following sample code into the
today.gofile:
package main
import (
"fmt"
"time"
)
func today() {
// Code to be completed
}
func main() {
today()
}
This code sets up the basic structure of the program, including the today() function that you will need to complete in the next step.
Implement the today() Function
In this step, you will implement the today() function to output the current day of the week and determine if it is Wednesday. Follow the steps below to complete this step:
- In the
today.gofile, locate thetoday()function. - Inside the
today()function, add the following code:
now := time.Now()
fmt.Println("Today is", now.Weekday())
if now.Weekday() == time.Wednesday {
fmt.Println("The day is Wednesday")
}
This code uses the time.Now() function to get the current time, and then uses the Weekday() method to get the current day of the week. It then prints the day of the week and checks if it is Wednesday, printing an additional message if it is.
- Save the
today.gofile.
Run the Program
In this step, you will run the program to see the output. Follow the steps below to complete this step:
- Open a terminal or command prompt and navigate to the
~/projectdirectory. - Run the following command to execute the program:
go run today.go
You should see output similar to the following:
Today is Sunday
If today is Wednesday, you should also see the following output:
The day is Wednesday
Congratulations! You have completed the project and learned how to create a Go program that outputs the current day of the week and determines if it is Wednesday.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



