交替打印 依次打印
https://mp.weixin.qq.com/s/I5va3PI1oGIj8R_n3Nw2yw
1115. 交替打印 FooBar - 力扣(LeetCode) https://leetcode.cn/problems/print-foobar-alternately/description/
func main() {
    odd := make(chan struct{}, 1)
    even := make(chan struct{}, 1)
odd <- struct{}{}
wg := sync.WaitGroup{}
    wg.Add(2)
    a := func() {
        for i := 1; i <= 9; i += 2 {
            <-odd
            print(i, "\n")
            even <- struct{}{}
        }
        wg.Done()
    }
    b := func() {
        for i := 2; i <= 9; i += 2 {
            <-even
            print(i, "\n")
            odd <- struct{}{}
        }
        wg.Done()
    }
    go a()
    go b()
    wg.Wait()
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号