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
+5k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+7.8k Golang : Configure Apache and NGINX to access your Go service example
+12k Golang : Forwarding a local port to a remote server example
+13.2k Golang : Activate web camera and broadcast out base64 encoded images
+5.7k Javascript : Get operating system and browser information
+25.9k Golang : Calculate future date with time.Add() function
+27.7k Golang : Connect to database (MySQL/MariaDB) server
+9.1k Golang : Get all countries currencies code in JSON format
+10.4k Golang : Removes punctuation or defined delimiter from the user's input
+5.4k Golang : Error handling methods
+8.9k Golang : How to get username from email address
+16.7k Golang : Find file size(disk usage) with filepath.Walk