怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面编程网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《Golang - 如何合并多个通道?》,涉及到,有需要的可以收藏一下
问题内容尝试安排项目,将项目发送到“状态”通道以获取状态,然后将项目发送到“删除”通道以删除项目。我正在设置两个通道的大小,预计在项目删除完成后我会“完成”。看起来代码在执行“删除”之后就停止了,然后才“完成”。为什么我会收到“致命错误:所有 goroutine 都在睡觉 - 死锁!”就在“删除项目”之后和“完成”之前吗?哪个 goroutine 正在睡觉?
https://go.dev/play/p/w49nif5xngq
package main
import (
"fmt"
"time"
)
func main() {
numbers := []int{1, 2, 4}
for _, n := range numbers {
fmt.Printf("Schedule and delete %d items.\n", n)
statusChan := make(chan string, n)
deleteChan := make(chan string, n)
done := make(chan bool)
go func(n int, statusChan chan<- string) {
for i := 0; i < n; i++ {
i := i
go func(n int, statusChan chan<- string) {
fmt.Printf("Scheduling item number ... %d\n", i)
itemNum := fmt.Sprintf("item_num_%d\n", i)
time.Sleep(500 * time.Millisecond)
statusChan <- itemNum
}(n, statusChan)
}
}(n, statusChan)
go func(statusChan <-chan string, deleteChan chan<- string) {
for itemNum := range statusChan {
fmt.Printf("Checking status of item number ... %s\n", itemNum)
time.Sleep(500 * time.Millisecond)
deleteChan <- itemNum
}
}(statusChan, deleteChan)
go func(deleteChan <-chan string, done chan<- bool) {
for itemNum := range deleteChan {
fmt.Printf("Delete item: %s", itemNum)
time.Sleep(500 * time.Millisecond)
}
fmt.Printf("Done with scheduling and deleting %d item.\n", n)
done <- true
}(deleteChan, done)
<-done
}
}
正确答案
这似乎符合您的想法。我使用“***”作为“全部完成”信号。
package main
import (
"fmt"
"time"
)
func main() {
numbers := []int{1, 2, 4}
for _, n := range numbers {
fmt.Printf("Schedule and delete %d items.\n", n)
statusChan := make(chan string, n)
deleteChan := make(chan string, n)
done := make(chan bool)
go func(n int, statusChan chan<- string) {
for i := 0; i < n; i++ {
fmt.Printf("Scheduling item number ... %d\n", i)
itemNum := fmt.Sprintf("item_num_%d\n", i)
time.Sleep(500 * time.Millisecond)
statusChan <- itemNum
}
statusChan <- "***"
}(n, statusChan)
go func(statusChan <-chan string, deleteChan chan<- string) {
for itemNum := range statusChan {
fmt.Printf("Checking status of item number ... %s\n", itemNum)
deleteChan <- itemNum
}
}(statusChan, deleteChan)
go func(deleteChan <-chan string, done chan<- bool) {
for itemNum := range deleteChan {
if itemNum == "***" {
break
}
fmt.Printf("Delete item: %s", itemNum)
}
fmt.Printf("Done with scheduling and deleting %d item.\n", n)
done <- true
}(deleteChan, done)
<-done
}
}
以上就是《Golang - 如何合并多个通道?》的详细内容,更多关于的资料请关注编程网公众号!