koujianshusheng
V2EX  ›  Go 编程语言

关于 go 无缓冲 channel 的 deadlock 问题!

By koujianshusheng at 2022 年 5 月 27 日 · 2832 次点击

package main

import ( "fmt" "net" )

var temp net.IP

func main() { fmt.Println("start......") ch := make(chan int) <-ch fmt.Println("hello world") }

/* start......

*/

package main

import ( "fmt" )

func main() { fmt.Println("start......") ch := make(chan int) <-ch fmt.Println("hello world") }

/* start...... fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]: main.main() */

10 条回复  •  2022-05-27 19:11:08 +08:00
koujianshusheng
   1
koujianshusheng  
OP
   2022 年 5 月 27 日
一个堵塞一个死锁,就因为 net 包?
youngzy
   2
youngzy  
   2022 年 5 月 27 日
第一个应该也是死锁,没有创建新的协程,主协程阻塞。
去 play ground 试试?
koujianshusheng
   3
koujianshusheng  
OP
   2022 年 5 月 27 日
@youngzy 我是在本地环境运行的,play ground 会死锁,但是本地没有
codehz
   4
codehz  
   2022 年 5 月 27 日 via Android
引用 cgo 的包(包括 net )就会关闭死锁检测器
koujianshusheng
   5
koujianshusheng  
OP
   2022 年 5 月 27 日
@codehz 这个说法有参考资料吗,亲
iBugOne
   6
iBugOne  
   2022 年 5 月 27 日 via Android
koujianshusheng
   7
koujianshusheng  
OP
   2022 年 5 月 27 日
@iBugOne 感谢
lysS
   8
lysS  
   2022 年 5 月 27 日
无论是否 import net ,我这里都是 17 没有报错,18 报错
lysS
   9
lysS  
   2022 年 5 月 27 日
知道了,net 没有 cgo ,可以 import "C"来检测
zuokanyunqishi
   10
zuokanyunqishi  
   2022 年 5 月 27 日
go1.18.1 复现。。
The Go Programming Language
› http://golang.org/
› Go Playground
Go Projects
› Revel Web Framework
© 2026 V2EX · 70ms · 3.9.8.5