如何在几毫秒内获得Unix中的Unix时间?
我有以下功能:
func makeTimestamp() int64 {
return time.Now().UnixNano() % 1e6 / 1e3
}
Run Code Online (Sandbox Code Playgroud)
我需要更少的精度,只需要几毫秒.
我正在尝试实现随机时间睡眠(在Golang中)
r := rand.Intn(10)
time.Sleep(100 * time.Millisecond) //working
time.Sleep(r * time.Microsecond) // Not working (mismatched types int and time.Duration)
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算10分钟前的时间.为什么我不能用变量进行计算(可用于for循环).见 -
package main
import (
"fmt"
"time"
)
func main() {
// the time now
fmt.Println(time.Now())
// the time 50 minutes ago - WORKS
diff := (60 - 10) * time.Minute
newTime := time.Now().Add(-diff)
fmt.Println(newTime)
// the time 50 minutes ago - DOESN'T WORKS!
i := 10
diff = (60 - i) * time.Minute
newTime = time.Now().Add(-diff)
fmt.Println(newTime)
}
Run Code Online (Sandbox Code Playgroud)
为什么diff = (60 - i) * time.Minute不起作用?这是我得到的错误 -
prog.go:20: invalid operation: (60 - i) * time.Minute (mismatched types …Run Code Online (Sandbox Code Playgroud)