gitalk icon indicating copy to clipboard operation
gitalk copied to clipboard

Go Channel 详解

Open utterances-bot opened this issue 3 years ago • 3 comments

Go Channel 详解

Channel是Go中的一个核心类型,你可以把它看成一个管道,通过它并发核心单元就可以发送或者接收数据进行通讯(communication)。 它的操作符是箭头 <- 。 12ch <- v // 发送值v到Channel ch中v := <-ch // 从Channel ch中接收数据,并将数据赋值给v

(箭头的指向就是数据的流向) 就像 map 和 slice

https://colobu.com/2016/04/14/Golang-Channels/

utterances-bot avatar Sep 17 '21 09:09 utterances-bot

通过i, ok := <-c可以查看Channel的状态,判断值是零值还是正常读取的值。----好像在新版本的go上会报错:panic: close of closed channel

limzhang avatar Sep 17 '21 09:09 limzhang

range模块的代码 起个协程 睡一个小时 起的什么作用?

freeaswind avatar Jun 15 '22 01:06 freeaswind

"range模块的代码 起个协程 睡一个小时 起的什么作用?"

因为所有协程都执行完,而for循环还在等待 所以死锁了 fatal error: all goroutines are asleep - deadlock!

xionghengheng avatar May 16 '23 01:05 xionghengheng