正在学习Go,在此示例中,我可以看到该select语句让 goroutine 等待多个通信操作我们真的需要select声明吗?select我的下面没有声明也一样func runForChannel1(channel1 chan string) { time.Sleep(1 * time.Second) channel1 <- "Hi Arun ... I am Channel-1"}func runForChannel2(channel2 chan string) { time.Sleep(2 * time.Second) channel2 <- "Hi Arun ... I am Channel-2"}func testSelect() { channel1 := make(chan string) channel2 := make(chan string) go runForChannel1(channel1) go runForChannel2(channel2) chval1, chval2 := <-channel1, <-channel2 fmt.Println(chval1, chval2)}func main() { testSelect()}没有select声明,我能够等待两个渠道获得他们的价值......为什么我们需要Select声明?有人可以教育我吗?
- 2 回答
- 0 关注
- 106 浏览
添加回答
举报
0/150
提交
取消