package main import ( "fmt" "time" ) func main() { now := time.Now() year := now.Year() fmt.Println(year) }
package main import ( "fmt" "time" ) func main() { t := time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC) year := t.Year() fmt.Println(year) }This code creates a Time object representing January 1, 2021, and then calls the Year method to get the year component. The time package is a standard library package in Go.