Golang time.Duration type example

package time

Golang time.Duration type usage example. These constants are useful in calculating time lapsed, total time, etc. Instead of declaring them, just use from time package. For example, day := 24*time.Hour

 package main

 import (
  "fmt"
  "time"
 )

 func main() {
  fmt.Println("Nano second : ", time.Nanosecond)
  fmt.Println("Micro second : ", time.Microsecond)
  fmt.Println("Milli second : ", time.Millisecond)
  fmt.Println("Minute : ", time.Minute)
  fmt.Println("Hour : ", time.Hour)
  fmt.Println("Day : ", 24*time.Hour)

 }

Output :

 Nano second :  1ns
 Micro second :  1µs
 Milli second :  1ms
 Minute :  1m0s
 Hour :  1h0m0s
 Day :  24h0m0s

See also :

https://www.socketloop.com/tutorials/golang-how-to-declare-kilobyte-megabyte-gigabyte-terabyte-and-so-on

Reference :

http://golang.org/pkg/time/#Duration

Advertisement