golang channel 在接收值之前死锁

标签 go

我不明白为什么这不起作用 https://play.golang.org/p/_ALPii0pXV6但是这个https://play.golang.org/p/vCOjAr-o54e作品。

据我所知,goroutine 异步发送值 true 到 a 和 12 到 b。在 main 函数中, a 被阻塞,直到它接收到一个值。为什么当我重新安排它使 b 在 a 之前被阻塞时,会导致死锁?

最佳答案

默认情况下,Go channel 是无缓冲的。这意味着在接收方读取 channel 之前,它不能在 channel 上发送。这实际上是 Go 的首选模式。在大多数情况下,它比缓冲 channel 更有效。

这对您的第一个代码意味着 goroutine 在完成对 channel a 的写入之前无法继续写入 channel b。直到主 goroutine 读取 a.

关于golang channel 在接收值之前死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338051/

相关文章:

go - 将包含字符串的 []byte 转换为十进制值

go - 构建具有高可扩展性的 RESTful API

go - 在 main 中找不到包

linux - Golang 插件热重载

string - 如何在 Golang 中解决(字符串和字节类型不匹配)

go - 运行命令时如何从 Go 读取附加文件描述符

security - Printf 安全 golang

bash - Istanbul-tools 安装错误 : github. com/ethereum/go-ethereum/crypto/bn256/cloudflare.gfpMul : relocation target runtime. support_bmi2 not defined

database - 未找到实体时的正确错误处理

templates - 在你的 go 二进制文件中包含模板/html 文件