编程中的tick

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

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.

huangapple
  • 本文由 发表于 2012年9月23日 06:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/12548169.html
匿名

发表评论

匿名网友

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

确定