go - c :=make(chan int) and c:=make(chan int, 1)有什么区别?

标签 go channel

我认为它们是一样的,但是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" .

PlaygroundLink-WithBuffer

PlaygroundLink-WithoutBuffer

关于go - c :=make(chan int) and c:=make(chan int, 1)有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23233381/

相关文章:

Golang channel 输出顺序

去旅游#10 : What is the use of that done channel in the crawler solution

macos - golang 1.16 cgo/clang 在 darwin/arm64 上构建失败

go - 本地运行 Go Tour 时出错 : Couldn't find tour files: could not find go-tour content; check $GOROOT and $GOPATH

tcp - WebRTC 使用 TCP 还是 UDP?

android - 打开YouTube channel 调用YouTube应用(Android)

json - 为什么我从 mongodb 获取我的 json 中某些字段的所有零值?

reflection - 戈朗 : Function to determine the arity of functions?

go - 封闭 channel 与零 channel

go - 如何使用 channel 广播消息