代码如下,请大家猜一下运行结果
package main
import (
"fmt"
"time"
)
type St struct {
a chan int
}
func main() {
st := St{
a: make(chan int),
}
e := make(chan int)
go st.b(&e)
go st.c()
st.a <- 3
//st.a <- 5
e <- 10
time.Sleep(time.Second)
close(st.a)
fmt.Println(<-st.a)
}
func (s *St) c() {
for {
//fmt.Printf("%v\n", &s.a)
select {
case <-s.a:
fmt.Printf("c get a\n")
fmt.Printf("now is %v\n", time.Now().UnixNano())
default:
//continue
}
}
}
func (s *St) b(d *chan int) {
for {
select {
case <-s.a:
fmt.Printf("b func get \n")
fmt.Printf(" b func out put %v\n", &s.a)
case <-*d:
fmt.Println("b func get d")
//continue
}
}
}
有疑问加站长微信联系(非本文作者)