time.Duration
The Go type for a length of time, such as 2*time.Second, stored as a count of nanoseconds.
In more detail
Write durations with the unit constants, as in 30*time.Second, never as bare numbers. A Duration prints readably, like 2s or 1.5ms, and flag.Duration parses the same form from the command line.
Example
wait := flag.Duration("timeout", 2*time.Second, "deadline per attempt")Related terms
- time package — Go's standard library package for moments, durations, timers and formatting dates, built around the time.Time and time.Duration types.
- time.Since — The Go function that returns how much time has passed since a given moment, as a time.Duration.
- flag package — Go's standard library package for parsing command-line options into typed values with defaults and a generated usage message.
Taught in
- The select Statement — Concurrent Systems Programming with Go
- Context Cancellation and Timeouts — Concurrent Systems Programming with Go
- The time Package — Concurrent Systems Programming with Go
- Parsing Arguments with flag — Concurrent Systems Programming with Go
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go