英文:
Alternative Date/Time Libraries for Go
问题
Golang的time
包有没有替代品?我无法适应它笨拙的接口和奇怪的操作方式。这门语言整体上很好,但这部分对我来说从来没有搞明白过。
有人知道吗?一个非常好的、详细的教程也可以(我还没有找到一个合适的)。
我现在想做的是一个goroutine
,每秒只更新10次(或者任何我设置的变量间隔)。由于这个包不太好用,我还没有实现它。下面是伪代码。
function GoRoutine(updatesPerSecond int) {
interval = 1000msec / updatesPerSecond
for {
if enoughTimeHasPassed {
doThings()
}
}
}
英文:
Are there any alternatives to Golang's time
package? I can't come to grips with its clunky interface and strange way of doing things. The language overall is great, but this part of it just never clicked with me.
Anyone? A really good, thorough tutorial would work too (I have not managed to find one yet)
What I'm trying to do right now is a goroutine
that updates only 10 times per second (or any variable interval that I set it to). I've not yet implemented it, as the package is not playing nice. Here's the psuedo code.
function GoRoutine(updatesPerSecond int) {
interval = 1000msec / updatesPerSecond
for {
if enoughTimeHasPassed {
doThings()
}
}
}
答案1
得分: 7
你是否阅读了 http://golang.org/pkg/time/ 上的文档?
你需要使用一个 Ticker:
func Loop(fps int) {
t := time.NewTicker(time.Second / time.Duration(fps))
for t := range t.C {
fmt.Println("tick", t)
}
}
func main() {
go Loop(60)
time.Sleep(10 * time.Second)
}
然后像这样使用 go Loop(60)
。
英文:
Did you read the documentation at http://golang.org/pkg/time/?
You need to use a Ticker:
func Loop(fps int) {
t := time.NewTicker(time.Second / time.Duration(fps))
for t := range t.C {
fmt.Println("tick", t)
}
}
func main() {
go Loop(60)
time.Sleep(10 * time.Second)
}
then use it like go Loop(60)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论