英文:
tick in Programming go
问题
我在Go之旅网站上运行了以下代码:
package main
import "time"
import "fmt"
func main() {
c := time.Tick(1 * time.Minute)
for now := range c {
fmt.Printf("%v\n", now)
}
}
但是它显示了以下错误信息:
throw: 所有的goroutine都处于休眠状态 - 死锁!
goroutine 1 [chan receive]:
main.main()
/tmpfs/gosandbox-25c44134_87776a49_1b5620b3_abba0ea7_70540ccf/prog.go:8 +0x53
这是他们那边的问题还是我的代码有问题?
英文:
I run following code on a tour of go website
package main
import "time"
import "fmt"
func main() {
c := time.Tick(1 * time.Minute)
for now := range c {
fmt.Printf("%v\n", now)
}
}
But it said
throw: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
/tmpfs/gosandbox-25c44134_87776a49_1b5620b3_abba0ea7_70540ccf/prog.go:8 +0x53
Is it a problem of their side or problem in my code?
答案1
得分: 4
如果你在golang.org上运行这段代码,它是不会工作的。它的规则有些不同,并且不允许这种睡眠方式。相反,你需要在本地安装Go编译器并在自己的计算机上运行它。
另外,你可能想将time.Minute
更改为time.Second
,这样你就可以在不必等待整整一分钟的情况下看到它实际上在做什么。
英文:
[WorksForMe]
If you're running this code on golang.org it's not going to work. Its rules are a little different, and it doesn't allow this kind of sleeping. Instead install the Go compiler locally and run it on your own computer.
Also, you'll probably want to change time.Minute
to time.Second
so that you can see what it's actually doing without having to wait an entire minute.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论