使用两个go协程顺序打印一个字符串

代码

package main

import (
    "fmt"
    "strings"
    "sync"
    "time"
)

func main() {
    var wg sync.WaitGroup
    wg.Add(2)
    str := "hello,world!"
    str1 := []byte(str)
    sc := make(chan byte, len(str))
    count := make(chan int)
    for _, v := range str1 {
        sc <- v
    }
    close(sc)

    go func() {
        defer wg.Done()
        for {
            ball, ok := <-count
            if ok {
                pri, ok1 := <-sc
                if ok1 {
                    fmt.Printf("go 2 : %c\n", pri)
                } else {
                    close(count)
                    return
                }
                count <- ball
            } else {
                return
            }
        }
    }()

    go func() {
        defer wg.Done()
        for {
            time.Sleep(8 * time.Millisecond)
            ball, ok := <-count
            if ok {
                pri, ok1 := <-sc
                if ok1 {
                    fmt.Printf("go 1 : %c\n", pri)
                } else {
                    close(count)
                    return
                }
            } else {
                return
            }
            count <- ball
        }
    }()

    count <- 23
    wg.Wait()
} 

作者:研大石 https://www.bilibili.com/read/cv16615855/ 出处:bilibili