gpt4 book ai didi

goroutine 值返回顺序

转载 作者:数据小太阳 更新时间:2023-10-29 03:32:40 26 4
gpt4 key购买 nike

<分区>

为什么下面的代码总是返回 2,1,而不是 1,2。

func test(x int, c chan int) {
c <- x
}

func main() {
c := make(chan int)
go test(1, c)
go test(2, c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y)
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com