我是golang的新手,这是我的目的,我想让2个例程与一个公共频道同时运行,消费者应该在频道创建后启动并始终获取数据直到频道关闭,我的代码模板如下:
var userChannel chan string
for index := 0; index < *clientNums; index++ {
wg.Add(1)
go run1()
go run2()
}
wg.Wait()
}
func run1() {
defer wg.Done()
// ...some logic
userChannel = make(chan string, *readUserNums)
for index := 0; index < *readUserNums; index++ {
//...some logic
userChannel <- userId
//...some logic
}
close(userChannel)
}
func run2() {
for sendId := range userChannel {
//...some logic
}
}
在我的代码中,如果run2首先运行它会很恐慌,因为尚未创建通道并且通道中没有数据。我怎样才能达到我的目的?谢谢你
守候你守候我
米脂
相关分类