两个channel,一个写123,另一个写abc,最终输出a1b2c3。
package main
import (
"fmt"
"sync"
)
func main() {
arr := []string{"a", "b", "c"}
arr2 := []string{"1", "2", "3"}
signalCh := make(chan int)
signalCh2 := make(chan int)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for _, v := range arr {
fmt.Println(v)
signalCh <- 1
<-signalCh2
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for _, v := range arr2 {
<-signalCh
fmt.Println(v)
signalCh2 <- 1
}
}()
wg.Wait()
}