正在学习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声明?有人可以教育我吗?
萧十郎
慕运维8079593
相关分类