代码: https: //play.golang.org/p/Oh3oTa7GIPX
type a struct {
c chan bool
}
func (a *a) do() {
a.c <- true
}
type b struct {
c chan bool
a a
}
func main() {
b := b{
c: make(chan bool),
a: a{c: make(chan bool)},
}
go b.s()
b.c <- true
// below is to stay main gorutine alive
done := make(chan bool)
go func() {
time.Sleep(10 * time.Second)
done <- true
}()
for {
select {
case <-done:
fmt.Println("Done!")
return
}
}
}
func (b *b) s() {
for {
select {
case <-b.c:
fmt.Println("b c")
b.a.do()
case <-b.a.c:
fmt.Println("b a c")
}
}
}
上述实际输出是
b c
Done!
预期输出:
b c
b a c
Done !
我不明白为什么它不打印b a c?
代码是不言自明的,如果还需要更多详细信息,请询问
杨魅力
相关分类