私は Go を学んでおり、GoTours からこのレッスンに取り組んでいます。これが私がこれまでに持っているものです。
package main
import (
"fmt"
"code.google.com/p/go-tour/tree"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
if t != nil {
Walk(t.Left, ch)
ch <- t.Value
Walk(t.Right, ch)
}
}
func main() {
var ch chan int = make(chan int)
go Walk(tree.New(1), ch)
for c := range ch {
fmt.Printf("%d ", c)
}
}
ご覧のとおり、チャネルに書き込んだ値を出力して、Walk 関数をテストしようとしています。ただし、次のエラーが発生します。
1 2 3 4 5 6 7 8 9 10 throw: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
main.go:25 +0x85
goroutine 2 [syscall]:
created by runtime.main
/usr/local/go/src/pkg/runtime/proc.c:221
exit status 2
このエラーは、私close
がチャネルを使用したことがないため、予想されるはずです。ただし、このデッドロック エラーを「キャッチ」してプログラムで処理する方法はありますか?