Go Channel 面试题解析

24次阅读

共计 1159 个字符,预计需要花费 3 分钟才能阅读完成。

有一道这样的面试题目:
写代码实现两个 goroutine,其中一个产生随机数并写入到 go channel 中,另外一个从 channel 中读取数字并打印到标准输出。最终输出五个随机数。
凭直觉开始撸了以下代码:
func getRand(ch chan int) {
ch <- rand.Intn(5)
}
func printRand(ch chan int) {
fmt.Println(“Rand Number = “, <-ch)
}
func main() {
rand.Seed(63)
ch := make(chan int)
for i := 1; i <= 5; i++ {
go getRand(ch)
go printRand(ch)
}
}
这个是大部分人凭直觉写出来的,稍微有经验的就会发现问题。执行起来会什么都不打印程序就退出了,因为 main 方法没有等 goroutine 退出就结束了。下面改造一下 main 方法。
func main() {
rand.Seed(63)
ch := make(chan int)
for i := 1; i <= 5; i++ {
go getRand(ch)
go printRand(ch)
}
time.Sleep(3 * time.Second) // 加这一行等待 goroutine
close(ch)
}
现在至少输出达到要求了,但是还有问题。

等待 3 秒这个时间是拍脑袋写的,并不科学。生产条件下你怎么知道需要等多久呢?理想情况下是打印完 5 个数字两个 goroutine 都退出,所以我们需要一个更完善的退出机制。
退出机制由谁来触发?

func main() {
ch := make(chan int)
done := make(chan bool)
go func() {
for {
select {
case ch <- rand.Intn(5): // Create and send random number into channel
case <-done: // If receive signal on done channel – Return
return
default:
}
}
}()

go func() {
for i := 0; i < 5; i++ {
fmt.Println(“Rand Number = “, <-ch) // Print number received on standard output
}
done <- true // Send Terminate Signal and return
return
}()
<-done // Exit Main when Terminate Signal received
}
上面是一个比较完美的答案,一般推荐使用匿名方法创建 goroutine,通过 Done channel 来实现 goroutine 之间的通知符合 Golang 的思维模式。
思考:

为什么推荐匿名方法创建 goroutine?
除了 done channel 还有什么办法在 goroutine 间传递信息?
这个问题是不是经典的生产者 / 消费者问题

正文完
 0