package main import ( "fmt" "time" ) func main() { t1 := time.Now() t2 := time.Now().Add(time.Hour) if t1.Before(t2) { fmt.Println("t1 is before t2") } else { fmt.Println("t2 is before t1") } }
package main import ( "fmt" "time" ) func main() { t1 := time.Date(2021, time.December, 25, 0, 0, 0, 0, time.UTC) t2 := time.Date(2021, time.December, 26, 0, 0, 0, 0, time.UTC) if t1.Before(t2) { fmt.Println("Christmas is before Boxing Day") } else { fmt.Println("Boxing Day is before Christmas") } }In this example, the Before method is used to compare two specific dates, Christmas and Boxing Day, using the time.Date() function. The output of the program is "Christmas is before Boxing Day" because t1 occurs before t2. Overall, the time.Time.Before method is a useful tool for comparing time values in Go programs.