代码是这个样子的,我就是想用用并发的功能做个小样子,结果一直报错,报错信息见代码下方
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
server := make(chan int)
for i := 0; i < 3; i++ {
time.Sleep(time.Second)
wg.Add(1)
go func(id int) {
fmt.Println("A Customer is Coming")
<-server
fmt.Println("A Customer is Being Served")
time.Sleep(2 * time.Second)
fmt.Println("Service is finished, the customer is leaving")
server <- 1
wg.Done()
}(i)
}
for i := 0; i < 2; i++ {
server <- 1
}
wg.Wait()
close(server)
fmt.Println("Over")
}
错误信息:fatal error: all goroutines are asleep - deadlock!
有疑问加站长微信联系(非本文作者)