Module 8 · Lesson 4 of 539/50 in the course~12 min
Module lessons (4/5)
The package `time`
Lesson preview5 hands-on exercises readyCourse Go · Module 8 — Essential standard library
time.Now(), durations, parsing with the reference layout 2006-01-02 15:04:05, timers and tickers.
What you'll learn in this lesson
- Distinguish and combine
time.Timeandtime.Durationusing the multiplicative constants (time.Millisecond,time.Second). - Measure execution times accurately and monotonically with
time.Since. - Format and parse dates with the Go reference date (
2006-01-02 15:04:05) or with ready constants (time.RFC3339). - Create
time.NewTickeravoiding resource leaks viadefer ticker.Stop().
Overview
The time package clearly separates two concepts that are confused in many application bugs: instants (time.Time, specific points on the timeline) and durations (time.Duration, intervals between two instants).
A time.Time is immutable: methods like Add, UTC, and Truncate return a new value without touching the original.
Sections covered in this lesson
- 1
time.Timeand safe comparisons - 2
time.Durationand measurements withtime.Since - 3The Go reference layout (
2006) - 4Periodic Timers with
time.NewTicker - 5Guided practice
- 6Check for understanding
Pro Lesson · Module 8 — Essential standard library
Unlock "The package `time`" and the full Course Go curriculum
`time.Now()`, durations, parsing with the reference layout `2006-01-02 15:04:05`, timers and tickers.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Go plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.