【发布时间】:2019-05-14 02:51:36
【问题描述】:
为什么像下面这样的 GO 例程在使用缓冲通道时会以随机顺序输出字节序列?
这里是复制错误行为的代码,其中data.csv 是一个简单的 CSV,其中包含 1000 行随机数据(每行大约 100 个字节)加上标题行(总共 1001 行)。
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))
}
以下是输出的前 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
[...]
另一方面,如果 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
[...]
数据是随机生成的。
【问题讨论】: