通道没有死锁或锁定

问题描述 投票:3回答:2

我有一小段这样的代码

func main() {
    var c chan string
    go func() {
        c <- "let's get started"//write1
        //c <- "let's get started"//write2
        //c <- "let's get started"//write3
        fmt.Println("wrote the stuff....")
    }()
    //time.Sleep(3 * time.Second) //adding this always shows fatal exception
    c = make(chan string)
    fmt.Println(<-c)
}

如果我取消注释wrote the stuff....//write2代码片段行,我在控制台上看不到输出write3。我知道可能我不认为这是因为通道是无缓冲且完全同步的,并且通道外只有一次读取。然而,当程序退出时,go例程被阻止,为什么没有像deadlocked...这样的错误或者我看到的这种情况?

go
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.