Time durations
Express durations with explicit units or parsing; raw integers often become nanoseconds by accident.
Canonical guidance
- use constants like
500 * time.Millisecond - parse human-entered durations with
time.ParseDuration - be explicit at boundaries where config uses integers
Use when
- timeouts and retries
- intervals and backoff
- config parsing
Avoid
time.Duration(5)when you mean seconds- ambiguous integer config without documented units
- scattering ad hoc conversion logic
Preferred pattern
client := &http.Client{
Timeout: 5 * time.Second,
}
Anti-pattern
- reading
5from config and turning it intotime.Duration(5)without multiplying by a unit
Explanation: This anti-pattern is tempting because time.Duration is an integer type, but its default unit is nanoseconds, not whatever the reader hoped.
Why
- time bugs often look fine in code review until they fail under real timing
Related pages
Sources
- time package - Go Team
- Providing a wrong time duration (#75) - Teiva Harsanyi