为什么这棵树行走功能会死锁?

时间:2018-06-14 09:30:27

标签: go tree

我正在通过Tour of Go来恢复我的记忆,并且我在等效二元树练习上遇到了麻烦。我编写了一些代码来遍历一个看似应该工作的二叉树。

package main

import (
    "golang.org/x/tour/tree"
    "fmt"
)

// 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 {
        return
    }
    ch <- t.Value
    Walk(t.Left, ch)
    Walk(t.Right, ch)
}

func main() {
    ch := make(chan int, 10)
    go Walk(tree.New(3), ch)
    for v := range ch {
        fmt.Printf("%q", v)
    }
}

当我运行上面的代码时,我收到以下错误:

'\x1e''\x0f''\t''\x03''\x06''\f''\x15''\x12''\x1b''\x18'fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
    /Users/james/src/local/go-sandbox/main.go:22 +0x115

Process finished with exit code 2

我想知道它为什么会死锁?在这种情况发生之前,它似乎会打印出一些垃圾。

2 个答案:

答案 0 :(得分:4)

您必须关闭该频道,因此它的范围循环将终止:

func main() {
    ch := make(chan int, 10)
    go func() {
        Walk(tree.New(3), ch)
        close(ch)
    }()
    for v := range ch {
        fmt.Printf("%q", v)
    }
}

答案 1 :(得分:1)

range循环将在ch关闭之前读取,但您永远不会将其关闭,因此您最终处于主要goroutine等待接收ch的状态,但是没有其他goroutines可以发送给它 - &#34;所有&#34;你的一个goroutines睡着了。

我只需将Walk电话打包成main,就像这样:

go func() {
    Walk(tree.New(3), ch)
    close(ch)
}()

请参阅Tour of Go条目,与您链接的条目相同,以便Range and Close进行复习。

相关问题