Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sleep and elapsed time

  • Sleep
  • Now
  • Sub
package main

import (
	"fmt"
	"time"
)

func main() {
	before := time.Now()
	fmt.Printf("%T\n", before)
	fmt.Println(before.UnixNano())

	time.Sleep(1000000) // 1 ms

	after := time.Now()
	fmt.Println(after.UnixNano())

	elapsed := after.Sub(before)
	fmt.Printf("Elapsed: %T  %v\n", elapsed, elapsed)
}
time.Time
1587754553086396917
1587754553087857409
Elapsed: time.Duration  1.460494ms