前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go channel 关闭和广播

Go channel 关闭和广播

作者头像
王小明_HIT
发布2021-04-30 11:24:20
1.1K0
发布2021-04-30 11:24:20
举报
文章被收录于专栏:程序员奇点

Go channel 关闭和广播

代码语言:javascript
复制
Dont Communicate by sharing memory, sharing memory by communicate.
不要通过共享内存的方式通讯,要通过通讯的方式共享内存

Channel 基本概念

一个通道相当于 FIFO 的队列, 通道中各个元素之间都是严格按照发送的顺序队列,先发送的队列一定会被先接收,元素值的发送和传输和发送都使用到操作符 <-

channel 的关闭

  • 向关闭的 channel 发送数据, 会导致 panic
代码语言:javascript
复制
package main

import (
 "fmt"
 "sync"
 "testing"
)

func dataProducer(ch chan int, wg *sync.WaitGroup) {
 go func() {
  for i := 0; i < 11; i++ {
   ch <- i
  }
  wg.Done()
 }()
}

func dataConsumer(ch chan int , wg *sync.WaitGroup) {
 go func () {
  for i := 0; i < 10; i++ {
   data := <- ch
   fmt.Println(data)
  }
  close(ch)
  wg.Done()
 }()
}

func TestCloseChanner(t *testing.T) {
 var wg sync.WaitGroup
 ch := make(chan int)

 wg.Add(1)
 dataProducer(ch, &wg)
 wg.Add(1)
 dataConsumer(ch, &wg)
 wg.Wait()
}

运行结果
代码语言:javascript
复制
GOROOT=C:\Go #gosetup
GOPATH=D:\GoWorkSpace #gosetup
C:\Go\bin\go.exe test -c -o C:\Users\wangm\AppData\Local\Temp\___channel_close_test_go.exe GoProject/src/main/gobase/channel #gosetup
C:\Go\bin\go.exe tool test2json -t C:\Users\wangm\AppData\Local\Temp\___channel_close_test_go.exe -test.v -test.run ^\QTestCloseChanner\E$ #gosetup
=== RUN   TestCloseChanner
0
1
2
3
4
5
6
7
8
9
panic: send on closed channel

goroutine 20 [running]:
GoProject/src/main/gobase/channel.dataProducer.func1(0xc0000862a0, 0xc0000942b0)
 D:/GoWorkspaces/GoProject/src/main/gobase/channel/channel_close_test.go:12 +0x4a
created by GoProject/src/main/gobase/channel.dataProducer
 D:/GoWorkspaces/GoProject/src/main/gobase/channel/channel_close_test.go:10 +0x50
Process finished with exit code 1
  • v, ok <- ch; ok 为 bool 值, true 表示正常接收,false 表示通道关闭
  • 所有的 channel 接收者都会在 channel 关闭时,立刻从阻塞等待中返回且上述 ok 值 为 false 。这个广播机制被利用,进行多个订阅同时发送信号。
代码语言:javascript
复制
package main

import (
 "fmt"
 "sync"
 "testing"
)

func dataProducer(ch chan int, wg *sync.WaitGroup) {
 go func() {
  for i := 0; i < 10; i++ {
   ch <- i
  }
  close(ch)
  wg.Done()
 }()
}

func dataConsumer(ch chan int , wg *sync.WaitGroup) {
 go func () {
  for i := 0; i < 10; i++ {
   if data, ok := <- ch; ok {
    fmt.Println(data)
   } else {
    break
   }
  }
  wg.Done()
 }()
}

func TestCloseChanner(t *testing.T) {
 var wg sync.WaitGroup
 ch := make(chan int)

 wg.Add(1)
 dataProducer(ch, &wg)
 wg.Add(1)
 dataConsumer(ch, &wg)
 wg.Wait()
}

运行结果

代码语言:javascript
复制
GOROOT=C:\Go #gosetup
GOPATH=D:\GoWorkSpace #gosetup
C:\Go\bin\go.exe test -c -o C:\Users\wangm\AppData\Local\Temp\___channel_close_test_go.exe GoProject/src/main/gobase/channel #gosetup
C:\Go\bin\go.exe tool test2json -t C:\Users\wangm\AppData\Local\Temp\___channel_close_test_go.exe -test.v -test.run ^\QTestCloseChanner\E$ #gosetup
=== RUN   TestCloseChanner
0
1
2
3
4
5
6
7
8
9
--- PASS: TestCloseChanner (0.00s)
PASS

Process finished with exit code 0

测试代码
代码语言:javascript
复制
package main

import (
 "fmt"
 "sync"
 "testing"
)

func dataProducer(ch chan int, wg *sync.WaitGroup) {
 go func() {
  for i := 0; i < 10; i++ {
   ch <- i
  }
  close(ch)
  wg.Done()
 }()
}

func dataConsumer(ch chan int , wg *sync.WaitGroup) {
 go func () {
  for i := 0; i < 11; i++ {
   data := <- ch
   fmt.Println(data)
  }
  wg.Done()
 }()
}

func TestCloseChanner(t *testing.T) {
 var wg sync.WaitGroup
 ch := make(chan int)

 wg.Add(1)
 dataProducer(ch, &wg)
 wg.Add(1)
 dataConsumer(ch, &wg)
 wg.Wait()
}

运行结果:

代码语言:javascript
复制
GOPATH=D:\GoWorkSpace #gosetup
C:\Go\bin\go.exe test -c -o C:\Users\wangm\AppData\Local\Temp\___channel_close_test_go.exe GoProject/src/main/gobase/channel #gosetup
C:\Go\bin\go.exe tool test2json -t C:\Users\wangm\AppData\Local\Temp\___channel_close_test_go.exe -test.v -test.run ^\QTestCloseChanner\E$ #gosetup
=== RUN   TestCloseChanner
0
1
2
3
4
5
6
7
8
9
0
--- PASS: TestCloseChanner (0.00s)
PASS

Process finished with exit code 0

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员奇点 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Go channel 关闭和广播
    • Channel 基本概念
      • channel 的关闭
        • 运行结果
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档