通道发送到哪里有什么问题

What is the problem about where channel send

这个程序输出:

    fatal error: all goroutines are asleep - deadlock!
    goroutine 1 [chan send]:
    main.main()
    /home/user/go/src/examples/run/chan1.go:51 +0xa9

但是注释第 5 行 c <- 1 和取消注释第 9 行 //c <- 1(或第 8 行 - 好的,这是有道理的)没有问题。这是管理渠道的意义还是困难

func main() {
    c := make(chan int)
    q := make(chan int)
    w := make(chan int)
    c <- 1
    go func() { q <- <-c}()
    go func() { w <- <-q}()
    // go func() {c <- 1}()
    //c <- 1
    fmt.Println(<-w)
}

我已经注释掉了代码的相关部分,所以它不太稳定。

package main

import "fmt"

func main() {
    c := make(chan int) // unbuffered channel
    q := make(chan int) // unbuffered channel
    w := make(chan int) // unbuffered channel

    // As c, q and w are unbuffered: so for a send to happen,
    // receiver must be ready or eventually ready. If somehow the
    // receiver can't be ready, then it's a deadlock because send
    // cannot happen.
    //
    // Example:
    // c := make(chan int)
    //
    // c <- 1   // send
    // <-c      // receive
    //
    // In the above example the send cannot happen as c <- 1
    // is a synchronous call i.e., it cannot proceed to next instruction
    // unless c <- 1 gets executed. And condition for the send to happen
    // is that their should be a receiver i.e., <-c
    // Hence, a deadlock

    // Spawn a goroutine so that c is ready
    // Non-blocking i.e., program can proceed to next instruction
    // even if q <- <-c is not executed.
    go func() {
        q <- <-c
    }()

    // Spawn a goroutine so that q is ready
    // Non-blocking i.e., program can proceed to next instruction
    // even if w <- <-q is not executed.
    go func() {
        w <- <-q
    }()

    // So, c is ready or eventually ready as it's handled by a already
    // spawned goroutine.
    c <- 100

    // c -> q -> w
    // There value sent to c will reach w
    fmt.Println(<-w)
}