Published on

How to check that a time value is a specific day of the week in Go?

How to check that a time value is a specific day of the week in Go?

time.Weekday() Function in Golang

In Go, we have time packages to determining as well as viewing time. Weekday() function in Go is used to check the English name of the day.

Weekday() functio Syntax in Go

    func (d Weekday) String() string

Example to check specific day of the week in Golang

    package main

    import (
        "fmt"
        "time"
    )

    func Example() {
        
        // The time package offers a time.Weekday() method which returns
        // a time.Weekday value	https://pkg.go.dev/time?tab=doc#Weekday

        now := time.Now()
        if now.Weekday() == time.Tuesday {
            fmt.Println("The day on Go playground is always Tuesday")
        }
        // Output:
        // The day on Go playground is always Tuesday
    }