请教一个goroutine报错问题,在《Go语言高级编程》有个并行版的素数筛子的例子,我想让主程序结束的时候,释放每个筛子所在的管道(资源),修改成如下代码,但是运行会有错误发生,意思是管道关闭后,又执行了写操作,但是不太清楚,为什么会发生这种情况,代码如下,请大牛分析一下原因是什么?
```go
package prime
import (
"bytes"
"fmt"
"runtime"
"strconv"
"sync"
)
var wg1 = sync.WaitGroup{}
var chexit = make(chan bool)
func gen(name string) chan int {
ch := make(chan int)
wg1.Add(1)
go func() {
for i := 2; ; i++ {
select {
case ch <- i:
case <-chexit:
close(ch)
fmt.Println("go routine exit:", name)
wg1.Done()
return
}
}
}()
return ch
}
func primeFilter(chin chan int, prime int, name string) chan int {
chout := make(chan int)
wg1.Add(1)
go func() {
for {
select {
case v := <-chin:
if v%prime != 0 {
chout <- v
}
case <-chexit:
close(chout)
fmt.Println("go routine exit:", name)
wg1.Done()
return
}
}
}()
return chout
}
func Prime() {
ch := gen("generate numbers")
for i := 0; i < 100; i++ {
v := <-ch
//log.Printf("%d,%d\n", i+1, v)
ch = primeFilter(ch, v, "filter "+strconv.Itoa(i))
}
close(chexit)
wg1.Wait()
}
```
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)