我有一个 channel 接收从日志文件解析的事件,另一个 channel 用于同步。出于我的测试目的,有 8 个事件。
使用 for range
时语法,我得到 4 个事件。当使用已知数 (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
}
我一定是遗漏了与 channel 上的 for 循环相关的内容。
我试过添加 time.Sleep
就在 done <- true
之前部分。它没有改变结果。
最佳答案
当您使用 for range
时,每次循环迭代都会从 channel 中读取,并且您不会使用读取的值。因此,一半的值被丢弃。应该是:
for ev := range ch {
evtList = append(evtList, ev)
count++
}
为了实际利用循环迭代器中读取的值。
在 Tour of Go 中演示了通过 channel 进行测距并在 Go spec 中详细说明.
关于for-loop - 对于范围与静态 channel 长度 golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52764633/