永久循环读取两个通道的输出

I am working on the tree exercise of tour.golang. I have tried to implement the same function as written below.

func Same(t1, t2 *tree.Tree) bool {
    ch1 := make(chan int)
    ch2 := make(chan int)
    go Walk(t1, ch1);
    go Walk(t2, ch2);

    for c := range ch1 {
        d := <- ch2
        if c-d !=0 {
            return false
        }   

    }
    return true
}

Using the forever loop, I would like to compare if an output from ch1 is different from that of ch2. But the following is throwing this error:

fatal error: all goroutines are asleep - deadlock!

live version

You are seeing a deadlock for a very simple reason: you are ranging over ch1, but never closing it, so the for loop never terminates.

You could fix this by manually iterating over each tree only a certain number of times like your 0..10 loop in main():

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    ch1 := make(chan int)
    ch2 := make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)

    for i := 0; i < 10; i++ {
        c := <-ch1
        d := <-ch2
        if c-d != 0 {
            return false
        }

    }
    return true
}

Playground

Alternatively, you can alter the signature of Walk to accept a waitgroup argument that is incremented by the caller of Walk and decremented when each Walk returns along with a goroutine to close the channel once you're done walking:

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    if t.Left != nil {
        wg.Add(1)
        Walk(t.Left, ch, wg)
    }
    ch <- t.Value
    if t.Right != nil {
        wg.Add(1)
        Walk(t.Right, ch, wg)
    }

}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    ch1 := make(chan int)
    ch2 := make(chan int)

    var wg1 sync.WaitGroup
    wg1.Add(1)
    go Walk(t1, ch1, &wg1)
    go func() {
        wg1.Wait()
        close(ch1)
    }()

    var wg2 sync.WaitGroup
    wg2.Add(1)
    go Walk(t2, ch2, &wg2)
    go func() {
        // not strictly necessary, since we're not ranging over ch2, but here for completeness
        wg2.Wait()
        close(ch2)
    }()

    for c := range ch1 {
        d := <-ch2
        if c-d != 0 {
            return false
        }

    }
    return true
}

Playground

There is a problem here you are not sending a value to a channel for right subtree in walk function. But receiving it on other side that's why deadlock error. Because you are receiving a value from channel in case of right subtree which is never sent.

You should close the channel after walking the tree to terminate the range loop in case the trees are equal (to be aware of: Same returns true when the infix traversal of the trees is equal, their structure is not required to be equal).

func WalkTreeAndThenCloseChannel(t *tree.Tree, ch chan int) {
    Walk(t, ch)
    close(ch)
}

func Same(t1, t2 *tree.Tree) bool {
    ch1 := make(chan int)
    ch2 := make(chan int)
    go WalkTreeAndThenCloseChannel(t1, ch1);
    go WalkTreeAndThenCloseChannel(t2, ch2);

Note: You should check if the second channel has been closed in case the trees have a different number of items and a difference is not found ("thread starvation" would be a more appropriate term here than "deadlock").