Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
生产者用一些值填充通道并关闭它。在消费者方面,我想将所有值相加并在最后留下循环。我的解决方案如下所示:
total := 0 for { v, ok := <- ch if !ok { break } total += v }
有没有更优雅的方式?
只要生产者关闭通道,for/range 循环就会起作用。
total := 0 for v := range ch { total += v }
播放:http ://play.golang.org/p/cWcA57dnLC