package main
import (
"bytes"
"fmt"
"sync"
)
func main() {
printData := func(wg *sync.WaitGroup, data []byte) {
defer wg.Done()
var buffer bytes.Buffer
for _, b := range data {
fmt.Fprintf(&buffer, "%c", b)
}
fmt.Println(buffer.String())
}
var wg sync.WaitGroup
wg.Add(2)
data := []byte("Golang")
go printData(&wg, data[:3])
go printData(&wg, data[3:])
wg.Wait()
}
程序输出如下,
image.png
网友评论