英文:
Is there a function in go for timing work
问题
我是你的中文翻译助手,以下是翻译好的内容:
我基本上是一个Go的新手
我想用Go编写一个工作计时应用程序,可能会使用Fyne来进行GUI设计
找不到关于用于编写工作计时器的Go代码/库/模块的清晰信息
添加计时器、开始计时、停止计时等功能
我已经搜索了时间和计时器等相关内容,但没有找到适用于这个特定用例的示例。
提前感谢。
英文:
Pretty much a Go newby
I want to write an work timing application in go probably using Fyne for the GUI
Cannot find clear info on what Go Code / Library / Mod to use for doing work timers
Add Timer, Start Timer Stop Timer etc
I've searched for time and timers etc but no examples I can find fit this specific use case.
Thanks in advance.
答案1
得分: 2
没有什么魔法可以计时工作 - 只是简单地记录开始和结束时间:
func logDuration(name string, start time.Time) {
log.Printf("%q 花费了 %s", name, time.Since(start))
}
所以,例如,要记录函数的执行时间:
func x() {
defer logDuration("x()", time.Now()) // "now" 在 x() 开始时计算,但 defer 直到 x() 结束时才调用
// 做一些事情
}
https://play.golang.org/p/yxF4rCUny8l
英文:
There's no magic to timing work - just a simple capture of start & end time instances:
func logDuration(name string, start time.Time) {
log.Printf("%q took %s", name, time.Since(start))
}
so, for example, to log the duration of function:
func x() {
defer logDuration("x()", time.Now()) // "now" evaluated at start of x(), but defer not called until end of x()
// do stuff
}
答案2
得分: 0
我认为fyne在开箱即用时没有与时间相关的功能,我不确定它是否需要这样的功能。你可以使用标准库中的time包来轻松实现所需的一切,就像fyne应用页面的一个示例应用程序(https://gitlab.com/cchuster/speader/-/blob/main/main.go)中所做的那样。此外,你可以在这里了解如何使用定时器(https://gobyexample.com/timers)。
英文:
I do not think fyne has anything to work with time out of the box, and I'm not sure if it needs to have something like that.
You can easily implement everything you need using the time package from the standard library https://pkg.go.dev/time as it was done in one of the example apps from the fyne apps page https://gitlab.com/cchuster/speader/-/blob/main/main.go
Also, you can see how to work with timers here https://gobyexample.com/timers
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论