程序运行计时
package main
import (
“time”
)
func main() {
startTime := time.Now().UnixNano()<br /> /* 程序主体 */<br /> endTime := time.Now().UnixNano()
seconds:= float64((endTime - startTime) / 1e9)<br /> Milliseconds:= float64((endTime - startTime) / 1e6)<br /> nanoSeconds:= float64(endTime - startTime)
// 或者<br /> start:=time.Now()<br /> ....<br /> end:=time.Since(start) //该时间单位会自动调整<br />}
time.After倒计时
// 时间到了会往通道赋值
tchan := time.After(time.Second*3)
time.AfterFunc延迟函数
time.AfterFunc(time.Second*5, func() {
fmt.Println(“当前时间为:”, time.Now())
fmt.Println(“In AfterFunc …”)
})
time.NewTimer单次定时器
package main
import (
“fmt”
“time”
)
func main() {
timer := time.NewTimer(5 time.Second)
go func() {
fmt.Println(“当前时间为:”, time.Now())
t := <-timer.C
fmt.Println(“当前时间为:”, t)
}()
//停止计时器
timer.Stop()
for {
time.Sleep(1 time.Second)
}
}