通道/Go协程抛出错误

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

Channels/Goroutines throwing error

问题

我目前正在按照这个教程进行学习http://www.miek.nl/files/go/20120807-go.pdf,在第7章中讨论了通道/ goroutine。

然而,当我运行示例代码后,它给我抛出了一个错误。

这是你执行代码时的输出:

daniel:go> go run goroutines.go
Waiting...
Coffee is ready!
Tea is ready!
throw: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
/home/daniel/Dropbox/code/go/goroutines.go:21 +0xee

goroutine 2 [syscall]:
created by runtime.main
/build/buildd/golang-1/src/pkg/runtime/proc.c:221

goroutine 3 [chan send (nil chan)]:
main.ready(0x80bb0d4, 0x3, 0x2, 0x0)
/home/daniel/Dropbox/code/go/goroutines.go:13 +0xe5
created by main.main
/home/daniel/Dropbox/code/go/goroutines.go:18 +0x5e

goroutine 4 [chan send (nil chan)]:
main.ready(0x80bba30, 0x6, 0x1, 0x0)
/home/daniel/Dropbox/code/go/goroutines.go:13 +0xe5
created by main.main
/home/daniel/Dropbox/code/go/goroutines.go:19 +0x80

goroutine 5 [timer goroutine (idle)]:
created by addtimer
/build/buildd/golang-1/src/pkg/runtime/ztime_386.c:69
exit status 2

我的代码有什么问题吗?

任何帮助将不胜感激。

英文:

I'm currently following this tutorial http://www.miek.nl/files/go/20120807-go.pdf and on chapter 7 it discusses channels/goroutines

However the example code is throwing an error for me just after you run it.

package main

import (
	"fmt"
	"time"
)

var c chan int

func ready(w string, sec int) {
	time.Sleep(time.Duration(sec) * time.Second)
	fmt.Println(w, "is ready!")
	c <- 1
}

func main() {
	c := make(chan int)
	go ready("Tea", 2)
	go ready("Coffee", 1)
	fmt.Println("Waiting...")
	<-c
	<-c
}

Here is the output when you execute the code

daniel:go> go run goroutines.go 
Waiting...
Coffee is ready!
Tea is ready!
throw: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
	/home/daniel/Dropbox/code/go/goroutines.go:21 +0xee

goroutine 2 [syscall]:
created by runtime.main
	/build/buildd/golang-1/src/pkg/runtime/proc.c:221

goroutine 3 [chan send (nil chan)]:
main.ready(0x80bb0d4, 0x3, 0x2, 0x0)
	/home/daniel/Dropbox/code/go/goroutines.go:13 +0xe5
created by main.main
	/home/daniel/Dropbox/code/go/goroutines.go:18 +0x5e

goroutine 4 [chan send (nil chan)]:
main.ready(0x80bba30, 0x6, 0x1, 0x0)
	/home/daniel/Dropbox/code/go/goroutines.go:13 +0xe5
created by main.main
	/home/daniel/Dropbox/code/go/goroutines.go:19 +0x80

goroutine 5 [timer goroutine (idle)]:
created by addtimer
	/build/buildd/golang-1/src/pkg/runtime/ztime_386.c:69
exit status 2

Is there something wrong with my code?

Any help would be appreciated

答案1

得分: 6

是的,只是一个拼写错误:

package main

import (
	"fmt"
	"time"
)

var c chan int

func ready(w string, sec int) {
	time.Sleep(time.Duration(sec) * time.Second)
	fmt.Println(w, "已准备好!")
	c <- 1
}

func main() {
	c = make(chan int) // 之前是 c := make(chan int)
	go ready("茶", 2)
	go ready("咖啡", 1)
	fmt.Println("等待中...")
	<-c
	<-c
}

main() 没有使用全局的 c,因为它声明了一个新的。

请注意,你不必在 main() 函数内部创建通道,可以在声明时创建:

var c = make(chan int)
英文:

Yes, just a typo:

package main

import (
	&quot;fmt&quot;
	&quot;time&quot;
)

var c chan int

func ready(w string, sec int) {
	time.Sleep(time.Duration(sec) * time.Second)
	fmt.Println(w, &quot;is ready!&quot;)
	c &lt;- 1
}

func main() {
	c = make(chan int) // previously c := make(chan int)
	go ready(&quot;Tea&quot;, 2)
	go ready(&quot;Coffee&quot;, 1)
	fmt.Println(&quot;Waiting...&quot;)
	&lt;-c
	&lt;-c
}

main() didn't use the global c since it declared a new one.

Notice that you don't have to make the channel inside main(), make it while declaring it:

var c = make(chan int)

huangapple
  • 本文由 发表于 2012年8月8日 21:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/11865448.html
匿名

发表评论

匿名网友

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

确定