小编Sun*_*ine的帖子

一个关于 Go Channel 死锁的简单例子以及原因

我的代码如下:

package main

import (
    "fmt"
)

func main() {
    c1 := make(chan int)
    fmt.Println("push c1: ")
    c1 <- 10
    g1 := <- c1
    fmt.Println("get g1: ", g1)
}
Run Code Online (Sandbox Code Playgroud)

当我使用 delve 进行调试时,它会打印以下结果:

push c1:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
        D:/Go/projects/hello-world/src/ch9/code9_6/code1.go:10 +0xde
Process 6276 has exited with status 2
Run Code Online (Sandbox Code Playgroud)

我不知道为什么,这只是一个简单的渠道示例,我创建了一个渠道,向其发送价值并从中获取价值,仅此而已,有人可以告诉我为什么,以及如何纠正它,非常感谢。

channel go goroutine

4
推荐指数
1
解决办法
2653
查看次数

标签 统计

channel ×1

go ×1

goroutine ×1