GO例程中缓冲通道范围的输出损坏

Sim*_* M. 1 channel go goroutine

为什么像以下这样的GO例程在使用缓冲通道时以随机顺序输出字节序列?

下面是复制错误行为的代码,其中data.csv包含1000行随机数据(大约每行100个字节)加上标题行(总共1001行)的简单CSV.

package main

import (
    "bufio"
    "os"
    "time"
)

func main() {

    var channelLength = 10000
    var channel = make(chan []byte, channelLength)

    go func() {
        for c := range channel {
            println(string(c))
        }
    }()

    file, _ := os.Open("./data.csv")
    scanner := bufio.NewScanner(file)

    for scanner.Scan() {
        channel <- scanner.Bytes()
    }

    <-time.After(time.Second * time.Duration(3600))

}
Run Code Online (Sandbox Code Playgroud)

以下是输出的前6行作为我对"断开输出"的意思的一个例子:

979,C
tharine,Vero,cveror6@blinklist.com,Female,133.153.12.53
980,Mauriz
a,Ilett,milettr7@theguardian.com,Female,226.123.252.118
981
Sher,De Laci,sdelacir8@nps.gov,Female,137.207.30.217
[...]
Run Code Online (Sandbox Code Playgroud)

另一方面,如果channelLength = 0,则代码运行平稳,因此使用无缓冲通道(前6行):

id,first_name,last_name,email,gender,ip_address
1,Hebert,Edgecumbe,hedgecumbe0@apple.com,Male,108.84.217.38
2,Minor,Lakes,mlakes1@marriott.com,Male,231.185.189.39
3,Faye,Spurdens,fspurdens2@oakley.com,Female,80.173.161.81
4,Kris,Proppers,kproppers3@gmpg.org,Male,10.80.182.51
5,Bronnie,Branchet,bbranchet4@squarespace.com,Male,118.117.0.5
[...]
Run Code Online (Sandbox Code Playgroud)

数据是随机生成的.

Jim*_*imB 7

来自buffer.Scanner文档:

底层数组可能指向将被后续Scan扫描覆盖的数据

您在使用通过频道的切片时会遇到数据竞争.您需要复制您发送的数据.在这个例子中,通过使用string代替[]byte和调用最容易实现scanner.Text