相关疑难解决方法(0)

Go/golang time.Now().UnixNano()转换为毫秒?

如何在几毫秒内获得Unix中的Unix时间?

我有以下功能:

func makeTimestamp() int64 {
    return time.Now().UnixNano() % 1e6 / 1e3
}
Run Code Online (Sandbox Code Playgroud)

我需要更少的精度,只需要几毫秒.

time go date-conversion unix-timestamp

65
推荐指数
5
解决办法
9万
查看次数

如何在golang中实现随机睡眠

我正在尝试实现随机时间睡眠(在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)

random time sleep go

16
推荐指数
2
解决办法
1万
查看次数

我不能在golang中使用变量进行时间计算吗?

我正在尝试计算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)

time go calc

0
推荐指数
1
解决办法
1045
查看次数

标签 统计

go ×3

time ×3

calc ×1

date-conversion ×1

random ×1

sleep ×1

unix-timestamp ×1