## 问题:
以下代码可以正常执行,但是使用go run --race main.go会检测出竞争关系,理论上consume应该在close(another)后退出,此时不应该再和最后一行出现竞争,希望大家帮忙看一下问题在哪
``` golang
package main
import (
"fmt"
"time"
)
type test struct {
channel chan chan int
another chan int
}
func (t *test) consume() {
for v := range t.another {
c := <-t.channel
c <- v
}
fmt.Println("finished")
}
func main() {
test := test{}
test.channel = make(chan chan int, 10)
test.another = make(chan int, 5)
for i := 0; i < 10; i++ {
go func() {
ch := make(chan int)
for {
test.channel <- ch
select {
case i, ok := <-ch:
if !ok {
return
}
fmt.Println(i)
}
}
}()
}
go test.consume()
for i := 0; i < 100; i++ {
test.another <- i
}
close(test.another)
time.Sleep(time.Millisecond)
test.channel = make(chan chan int, 100)
}
```
有疑问加站长微信联系(非本文作者)