How to Use Time Package in Go: Syntax and Examples
In Go, the
time package provides functions and types to work with dates, times, durations, and formatting. You can get the current time with time.Now(), format it using Format(), and measure durations with time.Duration. Import it using import "time" to start using its features.Syntax
The time package offers types like Time for date and time, and Duration for time intervals. Common functions include:
time.Now(): gets current local timetime.Sleep(d time.Duration): pauses executiont.Format(layout string): formats time as stringtime.Parse(layout, value string): parses string to time
Use time.Duration to represent intervals like seconds or milliseconds.
go
package main import ( "fmt" "time" ) func main() { // Get current time now := time.Now() // Format time to string formatted := now.Format("2006-01-02 15:04:05") // Parse string back to time parsed, err := time.Parse("2006-01-02 15:04:05", formatted) if err != nil { fmt.Println("Error parsing time:", err) } // Duration example: 2 seconds d := 2 * time.Second fmt.Println("Now:", now) fmt.Println("Formatted:", formatted) fmt.Println("Parsed:", parsed) fmt.Println("Duration:", d) }
Example
This example shows how to get the current time, format it as a readable string, parse it back to a time object, and use a duration to pause execution.
go
package main import ( "fmt" "time" ) func main() { // Get current time now := time.Now() // Format time to string formatted := now.Format("2006-01-02 15:04:05") fmt.Println("Current time:", formatted) // Pause for 2 seconds fmt.Println("Sleeping for 2 seconds...") time.Sleep(2 * time.Second) fmt.Println("Awake now!") }
Output
Current time: 2024-06-15 12:00:00
Sleeping for 2 seconds...
Awake now!
Common Pitfalls
Common mistakes when using the time package include:
- Using the wrong layout string for formatting and parsing. Go uses a specific reference date
2006-01-02 15:04:05to define formats. - Confusing
time.Durationunits; always multiply bytime.Second,time.Millisecond, etc. - Ignoring errors returned by
time.Parse, which can cause unexpected zero times.
go
package main import ( "fmt" "time" ) func main() { // Wrong layout example (will cause error) _, err := time.Parse("01-02-2006", "06-15-2024") if err != nil { fmt.Println("Parsing error:", err) } // Correct layout correct, err := time.Parse("2006-01-02", "2024-06-15") if err == nil { fmt.Println("Parsed correctly:", correct) } }
Output
Parsing error: parsing time "06-15-2024" as "01-02-2006": cannot parse "06-15-2024" as "01"
Parsed correctly: 2024-06-15 00:00:00 +0000 UTC
Quick Reference
| Function/Type | Description |
|---|---|
| time.Now() | Returns current local time |
| time.Sleep(d time.Duration) | Pauses execution for duration d |
| t.Format(layout string) | Formats time t to string using layout |
| time.Parse(layout, value string) | Parses string to time using layout |
| time.Duration | Represents elapsed time, e.g., 2 * time.Second |
Key Takeaways
Import the time package with import "time" to access time functions.
Use time.Now() to get the current time and Format() to convert it to a string.
Always use Go's reference time layout "2006-01-02 15:04:05" for formatting and parsing.
Use time.Duration with units like time.Second to represent time intervals.
Check errors when parsing time strings to avoid unexpected results.