What Day Is It Today?

GoGoBeginner
Practice Now

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 time package in Go

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go/FunctionsandControlFlowGroup -.-> go/if_else("`If Else`") go/FunctionsandControlFlowGroup -.-> go/functions("`Functions`") subgraph Lab Skills go/if_else -.-> lab-301262{{"`What Day Is It Today?`"}} go/functions -.-> lab-301262{{"`What Day Is It Today?`"}} end

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:

  1. Open your preferred text editor or IDE.
  2. Create a new file named today.go in the ~/project directory.
  3. Copy the following sample code into the today.go file:
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:

  1. In the today.go file, locate the today() function.
  2. 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.

  1. Save the today.go file.

Run the Program

In this step, you will run the program to see the output. Follow the steps below to complete this step:

  1. Open a terminal or command prompt and navigate to the ~/project directory.
  2. 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.

Other Go Tutorials you may like