Time and Duration
Represent moments and intervals.
The time Package
Go's time package represents moments in time and spans between them.
time.Timeis a specific instant.time.Durationis a length of time.
package main
import (
"fmt"
"time"
)
func main() {
var d time.Duration = 2 * time.Second
fmt.Println(d)
}Getting the Current Time
time.Now() returns the current instant as a time.Time. (Its exact value depends on when the program runs.)
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Year())
}All lessons in this course
- Time and Duration
- Formatting and Parsing
- Timers and Tickers
- Time Zones