golang goroutine顺序循环打印ABC

package main

import (
    "sync"
)

var count = 5
func main(){

    wg:=sync.WaitGroup{}
    chanA :=make(chan struct{},1)
    chanB :=make(chan struct{},1)
    chanC :=make(chan struct{},1)


    chanA<- struct{}{}
    wg.Add(3)

    go printA(&wg,chanA,chanB)
    go printB(&wg,chanB,chanC)
    go printC(&wg,chanC,chanA)
    wg.Wait()
}

func printA(wg *sync.WaitGroup, chanA chan struct{}, chanB chan struct{}) {
    defer wg.Done()

    for i:=0;i
image.png

你可能感兴趣的:(golang goroutine顺序循环打印ABC)