1. Introduction to the LibraryIn Python, we treat time as a first-class citizen using datetime and pandas.Timestamp; in Go, time is divided into three components: time.Time, time.Duration, and time.Location. Consider Go’s time package as the “Swiss Army knife of time for Python programmers,” capable of performing tasks in a zero-dependency binary:• Parsing ISO-8601 timestamps from millions of log lines;• Converting Unix seconds to local time zones and writing to CSV on edge gateways;• Triggering backups in K8s Jobs according to cron rules;• Providing users with a message in the browser WASM saying “3 hours and 27 minutes left.”In short, with Go’s time package, you can handle “time” as easily as in Python while enjoying the advantages of static compilation, concurrency safety, and cross-platform single-file distribution.
2. Installing the Library
The Go standard library includes time, so no need for go get.
For experimental extensions, you can use: go get github.com/robfig/cron/v3
3. Basic Usage (4 Steps)
-
Current Timenow := time.Now()
-
Parsing a Stringt, _ := time.Parse(“2006-01-02 15:04:05”, “2024-08-20 23:59:59”)
-
Formatting Outputfmt.Println(t.Format(“2006/01/02 15:04”))
-
Time Zone Conversionloc, _ := time.LoadLocation(“America/New_York”)ny := t.In(loc)
4. Advanced Usage
• Timer: timer := time.NewTimer(5 * time.Second)
• Ticker heartbeat: tick := time.Tick(1 * time.Minute)
• Parsing Unix nanoseconds: time.Unix(0, 1692576000000000000)
• Custom format: layout := “Mon, 02 Jan 2006 15:04:05 MST”
5. Practical Application Scenarios
-
Log line filter: grep the last 30 minutes in a 100 GB log.
-
IoT data collection: Raspberry Pi writes temperature and humidity to CSV every 10 seconds, with the filename containing the local date.
-
Flash sale countdown: front-end WASM uses Go to calculate remaining time, reducing server load.
-
Household accounting: read WeChat CSV, grouping expenses by midnight daily.
6. In-depth Case Code: How Many Workdays Are Left Until the End of the Month
package main
import (
“fmt”
“time”
)
func workdaysLeft() int {
now := time.Now()
end := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second)
count := 0
for d := now; d.Before(end); d = d.AddDate(0, 0, 1) {
if wd := d.Weekday(); wd != time.Saturday && wd != time.Sunday {
count++
}
}
return count
}
func main() {
fmt.Printf(“There are %d workdays left this month\n”, workdaysLeft())
}
Go’s time package treats “time format” as a layout string, rather than using placeholders like %Y-%m-%d as in Python’s strftime.
The core rule: the layout must be a truncation or rearrangement of the reference time Mon Jan 2 15:04:05 MST 2006.
Below are the most commonly used formats listed as “source code → example output” for easy copying:

Usage Example:
t := time.Now()
// Formatting
fmt.Println(t.Format(“2006-01-02 15:04:05”))
// Parsing
s := “2024-08-20 18:30:45”
parsed, _ := time.Parse(“2006-01-02 15:04:05”, s)
fmt.Println(parsed)
Summary: The time package allows you, coming from a Python background, to seamlessly transition into the Go world, maintaining the intuition that “time is data” while gaining the benefits of static typing and concurrency safety.
Leave a comment about the time pitfalls you’ve encountered in Python, and let’s fill them together with 30 lines of Go code!