我认为它们是一样的,但是The go memory model中有一个这样的词:, 如果 channel 被缓冲(例如,c = make(chan int, 1)
)那么程序将不能保证打印“hello, world”——它可能会打印空字符串,崩溃,或者做其他事情。这是正确的吗?
最佳答案
虽然 Evan 是对的,但我认为更长的解释可能会有用:
如 Effective Go 中所述,以下是相同的,并为您提供无缓冲 channel :
ci := make(chan int) // unbuffered channel of integers
cj := make(chan int, 0) // unbuffered channel of integers
虽然具有任何其他值会给您一个缓冲 channel :
ck := make(chan int, 1) // buffered channel of integers
缓冲 channel
使用缓冲 channel ,Go 例程可以将值放入 channel ( ck <- 42
),然后继续执行下一条指令,而无需等待有人从 channel 读取。这是真的,除非 channel 缓冲区已满。
如果 channel 已满,Go 例程将等待另一个 Go 例程从 channel 读取,然后才能将自己的值放在那里。
无缓冲 channel
无缓冲 channel 将没有空间存储任何数据。因此,为了通过无缓冲 channel 传递值,发送 Go 例程将阻塞,直到接收 Go 例程收到该值。
因此,缓冲 channel 和非缓冲 channel 之间肯定存在差异。在内存模型情况下:
package main
import "fmt"
var c = make(chan int)
var a string
func f() {
a = "hello, world"
x := <- c
fmt.Println(x)
}
func main() {
go f()
c <- 0
print(a)
}
如果你有一个缓冲 channel var c = make(chan int, 1)
, main()
Go 例程只会将一个值放入缓冲区,然后继续 print(a)
。 ,也许在 f()
之前Go例程有时间设置a
至 "hello, world"
.
但在当前代码中,Go 主程序将阻塞在 c <- 0
处。 , 等待 f()
在继续打印之前接收值,然后我们确定 a
已设置为 "hello, world"
.
关于go - c :=make(chan int) and c:=make(chan int, 1)有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23233381/