gpt4 book ai didi

go - 使用 Go 计算耗时(以毫秒为单位)

转载 作者:行者123 更新时间:2023-12-01 22:38:47 26 4
gpt4 key购买 nike

我正在使用以下代码(Go 版本 1.12)来记录耗时:

package time

func Elapsed() func() {
start := time.Now()
return func() {
log.Debugf("Call took %v", time.Since(start))
}
}

这通常适用于 ms:

"elapsed":"945.714448ms"



但是,如果超过 1 秒,它会从 ms 变为 s:

"elapsed":"3.577901569s"



如何强制它始终以毫秒为单位登录?

enter image description here

最佳答案

您可以使用Milliseconds()DurationSince() 返回:

log.Debugf("Call took %vms", time.Since(start).Milliseconds())

请注意,这不会添加单位(如果您需要它)。

关于go - 使用 Go 计算耗时(以毫秒为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60311213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com