When testing two time.Time
variables for equality in Go, you may find that there are (ahem) times when you get surprising results if you don't use time.Equal()
:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
nowUTC := now.UTC()
fmt.Println("now: ", now.Location(), now.Format("15:04"))
fmt.Println("nowUTC:", nowUTC.Location(), " ", nowUTC.Format("15:04"))
fmt.Println("-----")
fmt.Printf("now == nowUTC: %t\n", now == nowUTC)
fmt.Printf("now.Equal(nowUTC): %t\n", now.Equal(nowUTC))
}
// Output:
// now: Local 23:00
// nowUTC: UTC 23:00
// -----
// now == nowUTC: false
// now.Equal(nowUTC): true
Run it in the Go Playground!
Top comments (0)