运行跟随 golang 片段时出现错误。我认为进度将在 wg.Wait() 处阻塞,直到 go 例程结束。那么该值将从 c1 获得。但它可能不会按预期进行。
func main() {
c1 := make(chan string)
//var c1 chan string
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
fmt.Printf("go routine begin\n")
time.Sleep(1 * time.Second)
c1 <- "one"
fmt.Printf("go routine done\n")
}()
wg.Wait()
fmt.Printf("done c1: %v\n", <-c1)
fmt.Printf("out\n")
}
错误信息是,
go routine begin
fatal error: all goroutines are asleep - deadlock!