## 问题:
以下代码可以正常执行,但是使用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)
}
```
```
package main
import (
"fmt"
"sync"
)
type test struct {
channel chan chan int
another chan int
}
func (t *test) consume() {
for v := range t.another {
select {
case c := <-t.channel:
c <- v
case <-exit:
break
}
}
fmt.Println("finished")
}
var wg=sync.WaitGroup{}
var exit =make(chan int,0)
func main() {
test := test{}
test.channel = make(chan chan int, 10)
test.another = make(chan int, 5)
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
ch := make(chan int)
ok:
for {
test.channel <- ch
select {
case v, ok := <-ch:
if !ok {
break
}
fmt.Println(v)
case <-exit:
break ok
}
}
wg.Done()
//fmt.Println("exit:",i)
}(i)
}
go test.consume()
for i := 0; i < 100; i++ {
test.another <- i
}
close(test.another)
close(exit)
wg.Wait()
test.channel = make(chan chan int, 100)
}
```
#1