Week numbers in Go (Golang)
To get the current ISO 8601 week number in Go (Golang), use the following code snippet:
_, week := time.Now().ISOWeek()
fmt.Println(week)
* Ensure your system or application regional settings are set to follow ISO 8601 standards (weeks start on Monday) if the output varies.
Step-by-Step Instructions
- Import the time package.
- Call time.Now().ISOWeek() which returns two values: the ISO year and the week number.
- Use both values together when formatting labels like 2026-W30.
Good to Know
ISOWeek() is in the standard library and fully ISO 8601 compliant. Remember the first return value is the ISO year, which is not always equal to time.Now().Year() around the new year.