分享
  1. 首页
  2. 文章

golang for-select 优雅的退出

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

  • 启动两个做为生产者的go-routing,
  • 分别将数据生产的数据写入两个带缓冲的通道cha1,ch2
  • 分别模拟在Main go-routing和 sub go-routing中通过for-select循环读取两个通道的值并打印
  • 通过3种方式优雅的退出main go-routing
exit for-select in go-routing

https://play.golang.org/p/Ar4389-87QE

package main
import (
 // "time"
 "fmt"
)
func produce(id int, ch chan string, quitSub chan int){
 for i:=0;i< 10;i++{
 ch <- fmt.Sprintf("msg:%d:%d",id,i)
 }
 quitSub <- id
}
func main() {
 c1 := make(chan string, 1) //定义两个有缓冲的通道,容量为1
 c2 := make(chan string, 1)
 quitForSelect := make(chan int, 2) //用于通知退出for select循环
 quitTag := []int{} //用于读取和存储quitForSlect通道的两个值,保证两个product都写入完毕
 quitMain := make(chan bool) //阻塞main gorouting,等待for select 处理完成
 go produce(1, c1, quitForSelect)
 go produce(2, c2, quitForSelect)
 go func(){
 for { //使用select来等待这两个通道的值,然后输出
 select {
 case msg1 := <- c1:
 fmt.Println(msg1)
 case msg2 := <- c2:
 fmt.Println(msg2)
 case q := <- quitForSelect: 
 fmt.Println("got quit tag for Gorouting id:", q)
 quitTag = append(quitTag,q)
 if len(quitTag) == 2{
 fmt.Println("end to quit Main ")
 quitMain <- true
 }
 }
 }
 }()
 <-quitMain
 fmt.Println("exit from main")
}
exit for-select by "break" in Main go-routing

https://play.golang.org/p/vEUvAslRqVw

package main
import (
 "fmt"
)
func produce(id int, ch chan string, quitSub chan int) {
 for i := 0; i < 10; i++ {
 ch <- fmt.Sprintf("msg:%d:%d", id, i)
 }
 quitSub <- id
}
func main() {
 c1 := make(chan string, 1) //定义两个有缓冲的通道,容量为1
 c2 := make(chan string, 1)
 quitForSelect := make(chan int, 2) //用于通知退出for select循环
 quitTag := []int{} //用于读取和存储quitForSlect通道的两个值,保证两个product都写入完毕
 go produce(1, c1, quitForSelect)
 go produce(2, c2, quitForSelect)
ForEnd:
 for { //使用select来等待这两个通道的值,然后输出
 select {
 case msg1 := <-c1:
 fmt.Println(msg1)
 case msg2 := <-c2:
 fmt.Println(msg2)
 case q := <-quitForSelect:
 fmt.Println("got quit tag for Gorouting id:", q)
 quitTag = append(quitTag, q)
 if len(quitTag) == 2 {
 fmt.Println("end to quit Main ")
 break ForEnd
 }
 }
 }
 fmt.Println("exit from main")
}
exit for-select by "goto" in Main go-routing

https://play.golang.org/p/GKD_NYpoMdT

package main
import (
 "fmt"
)
func produce(id int, ch chan string, quitSub chan int) {
 for i := 0; i < 10; i++ {
 ch <- fmt.Sprintf("msg:%d:%d", id, i)
 }
 quitSub <- id
}
func main() {
 c1 := make(chan string, 1) //定义两个有缓冲的通道,容量为1
 c2 := make(chan string, 1)
 quitForSelect := make(chan int, 2) //用于通知退出for select循环
 quitTag := []int{} //用于读取和存储quitForSlect通道的两个值,保证两个product都写入完毕
 go produce(1, c1, quitForSelect)
 go produce(2, c2, quitForSelect)
 for { //使用select来等待这两个通道的值,然后输出
 select {
 case msg1 := <-c1:
 fmt.Println(msg1)
 case msg2 := <-c2:
 fmt.Println(msg2)
 case q := <-quitForSelect:
 fmt.Println("got quit tag for Gorouting id:", q)
 quitTag = append(quitTag, q)
 if len(quitTag) == 2 {
 fmt.Println("end to quit Main ")
 goto ForEnd
 }
 }
 }
ForEnd:
 fmt.Println("go to here from for-select")
 fmt.Println("exit from main")
}

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

本文来自:简书

感谢作者:夜空一起砍猩猩

查看原文:golang for-select 优雅的退出

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

关注微信
9426 次点击 ∙ 4 赞
被以下专栏收入,发现更多相似内容
2 回复 | 直到 2021年08月25日 14:18:50
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏