这个技巧来自:https://twitter.com/davecheney/status/718020797734866944。计算时间差的代码如下:

package main
import (
    "time"
    "fmt"
)

func main()  {
    t1 := time.Now()
    time.Sleep(5 * time.Second)
    fmt.Println(time.Since(t1))
} 

执行结果如下:

5.0005s

time.Since(t)time.Now().Sub(t)shorthand.