Alternative Date/Time Libraries for Go

huangapple go评论92阅读模式
英文:

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).

huangapple
  • 本文由 发表于 2014年7月29日 07:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/25006155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定