Go中的Channel——range和select

本文介绍了Go语言中Channel的关键字使用,包括如何通过`range`自动检测channel关闭,以及如何利用`select`处理多个channel。通过实例展示了在并发编程中,如何优雅地处理蛋糕制作工厂的多口味蛋糕生产和装箱过程,强调了正确关闭channel的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据接受者总是面临这样的问题:何时停止等待数据?还会有更多的数据么,还是所有内容都完成了?我应该继续等待还是该做别的了?

对于该问题,一个可选的方式是,持续的访问数据源并检查channel是否已经关闭,但是这并不是高效的解决方式。Go提供了range关键字,将其使用在channel上时,会自动等待channel的动作一直到channel被关闭

 
  1. 示例代码1

  2. package main

  3. import (

  4. "fmt"

  5. "time"

  6. "strconv"

  7. )

  8.  
  9. func makeCakeAndSend(cs chan string, count int) {

  10. for i := 1; i <= count; i++ {

  11. cakeName := "Strawberry Cake " + strconv.Itoa(i)

  12. cs <- cakeName //send a strawberry cake

  13. }

  14. }

  15.  
  16. func receiveCakeAndPack(cs chan string) {

  17. for s := range cs {

  18. fmt.Println("Packing received cake: ", s)

  19. }

  20. }

  21.  
  22. func main() {

  23. cs := make(chan string)

  24. go makeCakeAndSend(cs, 5)

  25. go receiveCakeAndPack(cs)

  26.  
  27. //sleep for a while so that the program doesn’t exit immediately

  28. time.Sleep(3 * 1e9)

  29. }

 
  1. 输出结果

  2. Packing received cake: Strawberry Cake 1

  3. Packing received cake: Strawberry Cake 2

  4. Packing received cake: Strawberry Cake 3

  5. Packing received cake: Strawberry Cake 4

  6. Packing received cake: Strawberry Cake 5

我们告诉了蛋糕制作器我们需要5个蛋糕,但是蛋糕装箱器并不知道数目,而在之前版本的代码中,我们写死了具体的接收数目。上面的代码中,通过对channel使用range关键字,我们避免了给接收者写明要接收的数据个数这种不合理的需求——当channel被关闭时,接收者的for循环也被自动停止了

Channel and select

select关键字用于多个channel的结合,这些channel会通过类似于are-you-ready polling的机制来工作。select中会有case代码块,用于发送或接收数据——不论通过<-操作符指定的发送还是接收操作准备好时,channel也就准备好了。在select中也可以有一个default代码块,其一直是准备好的。那么,在select中,哪一个代码块被执行的算法大致如下:

  • 检查每个case代码块
  • 如果任意一个case代码块准备好发送或接收,执行对应内容
  • 如果多余一个case代码块准备好发送或接收,随机选取一个并执行对应内容
  • 如果任何一个case代码块都没有准备好,等待
  • 如果有default代码块,并且没有任何case代码块准备好,执行default代码块对应内容

在下面的程序中,我们扩展蛋糕制作工厂来模拟多于一种口味的蛋糕生产的情况——现在有草莓和巧克力两种口味!但是装箱机制还是同以前一样的。由于蛋糕来自不同的channel,而装箱器不知道确切的何时会有何种蛋糕放置到某个或多个channel上,这就可以用select语句来处理所有这些情况——一旦某一个channel准备好接收蛋糕/数据,select就会完成该对应的代码块内容

注意,我们这里使用的多个返回值case cakeName, strbry_ok := <-strbry_cs,第二个返回值是一个bool类型,当其为false时说明channel被关闭了。如果是true,说明有一个值被成功传递了。我们使用这个值来判断是否应该停止等待

 
  1. 示例代码2

  2. package main

  3.  
  4. import (

  5. "fmt"

  6. "time"

  7. "strconv"

  8. )

  9.  
  10. func makeCakeAndSend(cs chan string, flavor string, count int) {

  11. for i := 1; i <= count; i++ {

  12. cakeName := flavor + " Cake " + strconv.Itoa(i)

  13. cs <- cakeName //send a strawberry cake

  14. }

  15. close(cs)

  16. }

  17.  
  18. func receiveCakeAndPack(strbry_cs chan string, choco_cs chan string) {

  19. strbry_closed, choco_closed := false, false

  20.  
  21. for {

  22. //if both channels are closed then we can stop

  23. if (strbry_closed && choco_closed) { return }

  24. fmt.Println("Waiting for a new cake ...")

  25. select {

  26. case cakeName, strbry_ok := <-strbry_cs:

  27. if (!strbry_ok) {

  28. strbry_closed = true

  29. fmt.Println(" ... Strawberry channel closed!")

  30. } else {

  31. fmt.Println("Received from Strawberry channel. Now packing", cakeName)

  32. }

  33. case cakeName, choco_ok := <-choco_cs:

  34. if (!choco_ok) {

  35. choco_closed = true

  36. fmt.Println(" ... Chocolate channel closed!")

  37. } else {

  38. fmt.Println("Received from Chocolate channel. Now packing", cakeName)

  39. }

  40. }

  41. }

  42. }

  43.  
  44. func main() {

  45. strbry_cs := make(chan string)

  46. choco_cs := make(chan string)

  47.  
  48. //two cake makers

  49. go makeCakeAndSend(choco_cs, "Chocolate", 3) //make 3 chocolate cakes and send

  50. go makeCakeAndSend(strbry_cs, "Strawberry", 3) //make 3 strawberry cakes and send

  51.  
  52. //one cake receiver and packer

  53. go receiveCakeAndPack(strbry_cs, choco_cs) //pack all cakes received on these cake channels

  54.  
  55. //sleep for a while so that the program doesn’t exit immediately

  56. time.Sleep(2 * 1e9)

  57. }

 
  1. 输出结果

  2. Waiting for a new cake ...

  3. Received from Strawberry channel. Now packing Strawberry Cake 1

  4. Waiting for a new cake ...

  5. Received from Chocolate channel. Now packing Chocolate Cake 1

  6. Waiting for a new cake ...

  7. Received from Chocolate channel. Now packing Chocolate Cake 2

  8. Waiting for a new cake ...

  9. Received from Strawberry channel. Now packing Strawberry Cake 2

  10. Waiting for a new cake ...

  11. Received from Strawberry channel. Now packing Strawberry Cake 3

  12. Waiting for a new cake ...

  13. Received from Chocolate channel. Now packing Chocolate Cake 3

  14. Waiting for a new cake ...

  15. ... Strawberry channel closed!

  16. Waiting for a new cake ...

  17. ... Chocolate channel closed!

写在最后

实际上,有经验的Gopher一眼就能发现,示例代码1中的channel是没有正确关闭的,在for range语句的执行一直没有停止因为channel一直存在而没有被关闭,只不过随着time.Sleep()结束,main函数退出,所有的goroutine被关闭,该语句也被结束了而已

正确的解决步骤:
a)发送器一旦停止发送数据后立即关闭channel
b)接收器一旦停止接收内容,终止程序
c)移除time.Sleep语句

修改后代码:

 
  1. package main

  2.  
  3. import (

  4. "fmt"

  5. "strconv"

  6. )

  7.  
  8. func makeCakeAndSend(cs chan string, count int) {

  9. for i := 1; i <= count; i++ {

  10. cakeName := "Strawberry Cake " + strconv.Itoa(i)

  11. cs <- cakeName //send a strawberry cake

  12. }

  13. close(cs)

  14. }

  15.  
  16. func receiveCakeAndPack(cs chan string) {

  17. for s := range cs {

  18. fmt.Println("Packing received cake: ", s)

  19. }

  20. }

  21.  
  22. func main() {

  23. cs := make(chan string)

  24. go makeCakeAndSend(cs, 5)

  25. receiveCakeAndPack(cs)

  26. }

这样才是对channel使用range进行处理的优雅方法

同样的,第二个例子中,time.Sleep()语句可以去除掉,我们只需要让receiveCakeAndPack函数执行完毕后退出程序即可

修改后代码:

 
  1. package main

  2.  
  3. import (

  4. "fmt"

  5. "strconv"

  6. )

  7.  
  8. func makeCakeAndSend(cs chan string, flavor string, count int) {

  9. for i := 1; i <= count; i++ {

  10. cakeName := flavor + " Cake " + strconv.Itoa(i)

  11. cs <- cakeName //send a strawberry cake

  12. }

  13. close(cs)

  14. }

  15.  
  16. func receiveCakeAndPack(strbry_cs chan string, choco_cs chan string) {

  17. strbry_closed, choco_closed := false, false

  18.  
  19. for {

  20. //if both channels are closed then we can stop

  21. if strbry_closed && choco_closed {

  22. return

  23. }

  24. fmt.Println("Waiting for a new cake ...")

  25. select {

  26. case cakeName, strbry_ok := <-strbry_cs:

  27. if !strbry_ok {

  28. strbry_closed = true

  29. fmt.Println(" ... Strawberry channel closed!")

  30. } else {

  31. fmt.Println("Received from Strawberry channel. Now packing", cakeName)

  32. }

  33. case cakeName, choco_ok := <-choco_cs:

  34. if !choco_ok {

  35. choco_closed = true

  36. fmt.Println(" ... Chocolate channel closed!")

  37. } else {

  38. fmt.Println("Received from Chocolate channel. Now packing", cakeName)

  39. }

  40. }

  41. }

  42. }

  43.  
  44. func main() {

  45. strbry_cs := make(chan string)

  46. choco_cs := make(chan string)

  47.  
  48. //two cake makers

  49. go makeCakeAndSend(choco_cs, "Chocolate", 3) //make 3 chocolate cakes and send

  50. go makeCakeAndSend(strbry_cs, "Strawberry", 4) //make 3 strawberry cakes and send

  51.  
  52. //one cake receiver and packer

  53. receiveCakeAndPack(strbry_cs, choco_cs) //pack all cakes received on these cake channels

  54. }

### Golang 中 Channel 的用法与示例 ChannelGo 语言中的一个重要特性,用于在 Goroutines 之间安全地传递数据。它提供了一种简洁的方式来实现并发控制通信。 #### 创建初始化 Channel 可以使用 `make` 函数创建一个新的 channel。下面是一个简单的例子: ```go ch := make(chan int) ``` 此代码片段定义了一个整数类型的 channel[^1]。 #### 发送接收数据 通过 `<-` 运算符可以在 channel 上发送或接收数据。以下是基本操作的例子: ```go // 向 channel 发送数据 ch <- value // 从 channel 接收数据 value := <-ch ``` 这些语句分别表示向 channel 发送数据channel 获取数据的操作[^2]。 #### 使用带缓冲区的 Channel 无缓冲的 channel 只有当接收方准备好时才会继续执行;而带缓冲的 channel 则允许存储一定数量的数据项而不阻塞。可以通过指定第二个参数来设置缓冲大小: ```go bufferedCh := make(chan int, 10) ``` 这里创建了一个容量为 10 的带缓冲 channel[^3]。 #### 关闭 Channel 遍历其值 关闭一个 channel 表明不会再有任何新的值被写入其中。这通常发生在生产者完成所有工作之后。消费者可以通过 range 循环自动处理直到 channel 被关闭为止的所有值: ```go close(ch) for val := range ch { fmt.Println(val) } ``` 注意,在多生产者的场景下应小心管理谁负责关闭 channel,以免引发 panic 错误。 #### Select 语句支持多个通道操作 Go 提供了 select 语句用来等待多个 communication 操作之一变得可用。这是一个典型的非阻塞 I/O 实现方式: ```go select { case msg1 := <-ch1: fmt.Println("received", msg1) case ch2 <- "ping": default: fmt.Println("no activity") } ``` 上述代码展示了如何监听来自不同 channels 的消息或者尝试发送一条新消息给某个 channel。 #### 完整示例:计数器应用 以下展示的是利用 goroutine unbuffered channel 构建的一个简单计数器应用程序: ```go package main import ( "fmt" ) func counter(out chan<- int) { for i := 0; i < 100; i++ { out <- i // 将当前数值传送到 out channel } close(out) // 结束后关闭该 channel } func square(in <-chan int, out chan<- int) { for v := range in { // 遍历输入 channel 的每一个值 out <- v * v // 计算平方并传送至下一个 stage } close(out) // 处理完成后关闭 output channel } func printer(in <-chan int) { for v := range in { // 打印接收到的所有值 fmt.Println(v) } } func main() { naturals := make(chan int) squares := make(chan int) go counter(naturals) go square(naturals, squares) printer(squares) } ``` 在这个程序里,我们启动三个独立的任务——生成自然数序列、计算它们各自的平方以及打印最终的结果集。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isevena、

谢谢您的肯定

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值