代码点读机 / Go / time.Since

time.Since Go · 包功能

计算从某个时间点到现在的经过时长,返回 time.Duration。

怎么用

d := time.Since(开始时间)

小例子

package main
import (
    "fmt"
    "time"
)

func main() {
    start := time.Now()
    time.Sleep(10 * time.Millisecond)
    fmt.Println(time.Since(start) > 0)
}

运行结果

输出 true

提醒:常用来测量代码执行耗时

官方文档:https://pkg.go.dev/time#Since

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Go全部词条