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
+12.7k Golang : Chunk split or divide a string into smaller chunk example
+4.5k How to check with curl if my website or the asset is gzipped ?
+12k Facebook PHP getUser() returns 0
+11.1k Golang : How to check if a string starts or ends with certain characters or words?
+3.8k Golang : A program that contain another program and executes it during run-time
+12.6k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+15k Golang : Check if a string contains multiple sub-strings in []string?
+10.2k Golang : Generate DSA private, public key and PEM files example
+11.1k Golang : Extract part of string with regular expression
+7.1k Your page has meta tags in the body instead of the head
+6.1k Golang : Of hash table and hash map
+7.3k Golang : Implementing class(object-oriented programming style)