package main // 堆代码 duidaima.com import ( "fmt" "time" ) func main() { c1 := make(chan string) c2 := make(chan string) go func() { time.Sleep(1 * time.Second) c1 <- time.Now().String() }() go func() { time.Sleep(2 * time.Second) c2 <- time.Now().String() }() for i := 0; i < 2; i++ { select { case res1 := <-c1: fmt.Println("from c1:", res1) case res2 := <-c2: fmt.Println("from c2:", res2) } } } from c1: 2022-09-04 14:30:39.4469184 -0400 EDT m=+1.000172801 from c2: 2022-09-04 14:30:40.4472699 -0400 EDT m=+2.000524401上面的代码显示了select关键字是如何工作的:
package main import ( "fmt" "math/rand" "time" ) func main() { c1 := make(chan string) rand.Seed(time.Now().UnixNano()) for i := 0; i < rand.Intn(10); i++ { go func() { time.Sleep(1 * time.Second) c1 <- time.Now().String() }() } for { select { case res1 := <-c1: fmt.Println("from c1:", res1) } } }因为我们让一个随机数的goroutines运行,所以我们不知道有多少个作业。值得庆幸的是,底部包裹着select语句的for循环将捕获每一个输出。让我们看看如果我们运行这段代码会发生什么。
from c1: 2022-09-04 14:48:47.5145341 -0400 EDT m=+1.000257801 from c1: 2022-09-04 14:48:47.5146126 -0400 EDT m=+1.000336201 from c1: 2022-09-04 14:48:47.5146364 -0400 EDT m=+1.000359901 fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan receive]: main.main() /home/jacob/blog/testing/listening-to-multiple-channels-in-go/main.go:22 +0x128 exit status 2嗯,select 语句按预期收到了三次,但由于死锁,程序出错了。为什么会出现这种情况?记住,在没有任何条件的情况下,Go中的for循环会永远运行。这意味着 select 语句将永远尝试接收。然而,要运行的作业数量是有限的。即使没有更多的工作,select 语句仍然会尝试接收。
package main import ( "fmt" "math/rand" "sync" "time" ) func main() { c1 := make(chan string) exit := make(chan struct{}) rand.Seed(time.Now().UnixNano()) var wg sync.WaitGroup go func() { numJob := rand.Intn(10) fmt.Println("number of jobs:", numJob) for i := 0; i < numJob; i++ { wg.Add(1) go func() { defer wg.Done() time.Sleep(1 * time.Second) c1 <- time.Now().String() }() } wg.Wait() close(exit) }() for { select { case res1 := <-c1: fmt.Println("from c1:", res1) case <-exit: return } } } 3 from c1: 2022-09-04 15:09:08.6936976 -0400 EDT m=+1.000287801 from c1: 2022-09-04 15:09:08.6937788 -0400 EDT m=+1.000369101 from c1: 2022-09-04 15:09:08.6937949 -0400 EDT m=+1.000385101我们创建一个 exit channel 和一个WaitGroup。
package main import ( "fmt" "math/rand" "sync" "time" ) func main() { ashleyMsg := make(chan string) brianMsg := make(chan string) exit := make(chan struct{}) rand.Seed(time.Now().UnixNano()) var wg sync.WaitGroup go func() { numJob := rand.Intn(10) fmt.Println("number of jobs:", numJob) for i := 0; i < numJob; i++ { wg.Add(2) go func() { defer wg.Done() time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond) ashleyMsg <- "hi" }() go func() { defer wg.Done() time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond) brianMsg <- "what's up" }() } wg.Wait() close(exit) }() for { select { case res1 := <-ashleyMsg: fmt.Println("ashley:", res1) case res2 := <-brianMsg: fmt.Println("brian:", res2) case <-exit: fmt.Println("chat ended") return default: fmt.Println("...") time.Sleep(time.Millisecond) } } } ... number of jobs: 4 brian: what's up ... ashley: hi ... ... brian: what's up ashley: hi ashley: hi brian: what's up ... ... ashley: hi ... brian: what's up ... chat ended除了蹩脚的对话之外,我们可以看到默认情况下的工作方式。与其等待聊天记录的到来,我们可以在没有频道可以接收的时候做一些事情。在这个例子中,我们只是打印出省略号,但你可以做任何你想做的事情。