0PricingLogin
Go Academy · Lesson

Time and Duration

Represent moments and intervals.

The time Package

Go's time package represents moments in time and spans between them.

  • time.Time is a specific instant.
  • time.Duration is 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

  1. Time and Duration
  2. Formatting and Parsing
  3. Timers and Tickers
  4. Time Zones
← Back to Go Academy