go - 是否可以在 Golang 中使用“选择”访问通道 ch1、ch2?
问题描述
我试图调试这段代码,但被困在这里。我想访问 ch1, ch2 但发现什么也没打印。
package main
import (
"fmt"
)
type degen struct {
i, j string
}
func (x degen) CVIO(ch1, ch2 chan string, quit chan int, m, n string) {
for {
select {
case ch1 <- m:
fmt.Println(x.i)
case ch2 <- n:
fmt.Println("ok")
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
ch1 := make(chan string)
ch2 := make(chan string)
quit := make(chan int)
x := degen{"goosebump", "ok"}
go x.CVIO(ch1, ch2, quit, "goosebump", "ok")
}
期望:它应该打印要生成的通道数据。
解决方案
它不是很清楚你期望你的代码做什么:
main()
无需等待 go 例程退出就结束(很可能循环根本不会运行)。select
由于没有接收者,发送将不会继续(规范- “如果容量为零或不存在,则通道是无缓冲的,并且只有在发送者和接收者都准备好时通信才会成功。”)。- 没有任何东西发送到
quit
通道。
我怀疑以下(游乐场)可能会满足您的期望。
package main
import (
"fmt"
"sync"
)
type degen struct {
i, j string
}
func (x degen) CVIO(ch1, ch2 chan string, quit chan int, m, n string) {
for {
select {
case ch1 <- m:
fmt.Println(x.i)
case ch2 <- n:
fmt.Println("ok")
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
ch1 := make(chan string)
ch2 := make(chan string)
quit := make(chan int)
x := degen{"goosebump", "ok"}
var wg sync.WaitGroup
wg.Add(1)
go func() {
x.CVIO(ch1, ch2, quit, "goosebump", "ok")
wg.Done()
}()
<-ch1 // Receive from CH1 (allowing "ch1 <- m" in go routine to proceed)
<-ch2 // Receive from CH2 (allowing "ch2 <- n" in go routine to proceed)
quit <- 1
wg.Wait() // Wait for CVIO to end (which it should do due to above send)
}
推荐阅读
- azure - 如何在 azure 持久功能中取消正在运行的触发功能?
- r - 联合返回错误'没有适用于'unite_'的方法应用于类“c('double','numeric')”的对象
- javascript - 创建并返回在函数中创建的对象是否安全?
- python - Django Restframework 错误:获取属性错误和值错误
- ios - React Native TextInput 在 iOS 上渲染文本太高,切断某些字符的顶部
- server - GeneralSecurityException:无效的签名。(RewardedAdsVerifier - SSV)
- ubuntu - 上下文菜单(右键单击或工具栏)在 Visual Studio 代码 Ubuntu 上不显示任何内容
- algorithm - 在多边形内移动多边形
- sql-server - Azure DevOps 在单个版本部署中更新多个数据库
- service-worker - 如何预缓存来自 json 文件的 url 列表?