Golang 使用分片约束打印byte buffer

2019-06-13  本文已影响0人  FredricZhu
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
上一篇下一篇

猜你喜欢

热点阅读