find the current year in go

To find the current year in Go, we can use the time package. Specifically, we can use the Now() function to get the current time, and then extract the year component using the Year() method.

Here's an example:

main.go
package main

import (
    "fmt"
    "time"
)

func main() {
    currentYear := time.Now().Year()
    fmt.Println("Current year:", currentYear)
}
146 chars
12 lines

In this code, we first import the fmt and time packages. Then, we call the Now() function to get the current time, and extract the year component using the Year() method. Finally, we print the current year using fmt.Println().

related categories

gistlibby LogSnag