[英]for range vs static channel length golang
I have a channel taking events parsed from a log file and another one which is used for synchronization. 我有一个通道,它从一个日志文件中解析事件,而另一个则用于同步。 There were 8 events for the purpose of my test. 为了我的测试,有8个事件。
When using the for range
syntax, I get 4 events. 使用for range
语法时,我得到4个事件。 When using the known number (8), I can get all of them. 使用已知数字(8)时,我可以全部获得。
func TestParserManyOpinit(t *testing.T) {
ch := make(chan event.Event, 1000)
done := make(chan bool)
go parser.Parse("./test_data/many_opinit", ch, done)
count := 0
exp := 8
evtList := []event.Event{}
<-done
close(ch)
//This gets all the events
for i := 0; i < 8; i++ {
evtList = append(evtList, <-ch)
count++
}
//This only gives me four
//for range ch {
// evtList = append(evtList, <-ch)
// count++
//}
if count != exp || count != len(evtList) {
t.Errorf("Not proper lenght, got %d, exp %d, evtList %d", count, exp, len(evtList))
}
func Parse(filePath string, evtChan chan event.Event, done chan bool) {
log.Info(fmt.Sprintf("(thread) Parsing file %s", filePath))
file, err := os.Open(filePath)
defer file.Close()
if err != nil {
log.Error("Cannot read file " + filePath)
}
count := 0
scan := bufio.NewScanner(file)
scan.Split(splitFunc)
scan.Scan() //Skip log file header
for scan.Scan() {
text := scan.Text()
text = strings.Trim(text, "\n")
splitEvt := strings.Split(text, "\n")
// Some parsing ...
count++
evtChan <- evt
}
fmt.Println("Done ", count) // gives 8
done <- true
}
I must be missing something related to for loops on a channel. 我必须丢失一些与通道上的for循环有关的东西。
I've tried adding a time.Sleep
just before the done <- true
part. 我尝试添加time.Sleep
在done <- true
time.Sleep
部分之前。 It didn't change the result. 它并没有改变结果。
When you use for range
, each loop iteration reads from the channel, and you're not using the read value. 当for range
,每个循环迭代都从通道读取,并且您没有使用读取值。 Hence, half the values are discarded. 因此,将丢弃一半的值。 It should be: 它应该是:
for ev := range ch {
evtList = append(evtList, ev)
count++
}
In order to actually utilize the values read in the loop iterator. 为了实际利用循环迭代器中读取的值。
Ranging over channels is demonstrated in the Tour of Go and detailed in the Go spec . 围棋范围已在“ 围棋之旅”中进行了演示,并在围棋规范中进行了详细说明 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.