```
package main
import (
"fmt"
"sync"
)
var ordered1 chan struct{}
var ordered2 chan struct{}
func main(){
ordered1 = make(chan struct{})
ordered2 = make(chan struct{})
wg:=Wait(Job1,Job2,Job3)
wg.Wait()
fmt.Println("执行完毕")
}
func Job1() {
fmt.Println("JOB1")
ordered1<- struct{}{}
}
func Job2() {
<-ordered1
fmt.Println("JOB2")
defer func() {
ordered2<- struct{}{}
}()
}
func Job3() {
<-ordered2
defer fmt.Println("JOB3")
}
type JobFunction func()
func Wait(jobFuns ...JobFunction) *sync.WaitGroup {
var wg = &sync.WaitGroup{}
for _, fn := range jobFuns {
wg.Add(1)
go func(f func()) {
defer wg.Done()
f()
}(fn)
}
return wg
}
```
![Snipaste_2021-09-27_15-36-42.png](https://static.studygolang.com/210927/f3d741c047aeadfd152b07c24e839450.png)
有疑问加站长微信联系(非本文作者)