Golang : Get today's weekday name and calculate target day distance example
Let's calculate how long it will take to reach Friday from today using switch evaluation. The code sample below will get today's weekday and evaluate the sum of today + n (n is the number of days) before hitting Friday.
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now().Weekday()
fmt.Println("Today is ", today)
fmt.Println("When's Friday?")
switch time.Friday {
case today + 0:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.")
case today + 2:
fmt.Println("In two days.")
case today + 3:
fmt.Println("In three days.")
case today + 4:
fmt.Println("In four days.")
case today + 5:
fmt.Println("In five days.")
default:
fmt.Println("Will be sometime away.")
}
}
Sample output :
Today is Monday
When's Friday?
In four days.
Reference :
https://www.socketloop.com/references/golang-time-time-weekday-function-example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16k Golang : Set, Get and List environment variables
+4.1k Golang : Handling image beyond OpenCV video capture boundary
+14.9k How to enable MariaDB/MySQL logs ?
+3.1k JQuery : Calling a function inside Jquery(document) block
+3.9k Golang : Experimenting with the Rejang script
+6.7k Golang : Go as a script or running go with shebang/hashbang style
+9.9k Javascript : Prompt confirmation before exit
+15.7k Golang : Check if directory exist and create if does not exist
+13.1k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+6.6k Golang : How to control fmt or log print format?
+7.8k Golang : Sort and reverse sort a slice of runes
+10.8k Golang : Check if an integer is negative or positive