Go Channel 面试题解析

Airy · · 4225 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

有一道这样的面试题目:

写代码实现两个 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)
}

现在至少输出达到要求了,但是还有问题。

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

思考:

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

有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:Airy

查看原文:Go Channel 面试题解析

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

4225 次点击  
加入收藏 微博
被以下专栏收入,发现更多相似内容
1 回复  |  直到 2019-07-12 13:01:51
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传