请看以下代码:
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
begin := make(chan struct{})
c := make(chan struct{})
var token struct{}
sender := func() {
defer wg.Done()
<-begin
for i := 0; i < 2; i ++ {
fmt.Println("sender---", i)
c <- token
}
}
receiver := func() {
defer wg.Done()
<-begin
for i := 0; i < 2; i++ {
fmt.Println("receiver---", i)
<-c
}
}
wg.Add(2)
go sender()
go receiver()
close(begin)
wg.Wait()
}
问题:
1、begin这里已经阻塞了吗?可以正常运行吗?为什么?
2、如果能运行,请写出可能的运行结果?
|