dongqiu7365 2018-10-11 16:13
浏览 26
已采纳

对于范围与静态通道长度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.

When using the for range syntax, I get 4 events. When using the known number (8), I can get all of them.

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, "
")
            splitEvt := strings.Split(text, "
")
            // 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.

I've tried adding a time.Sleep just before the done <- true part. It didn't change the result.

  • 写回答

1条回答 默认 最新

  • dt3674 2018-10-11 16:41
    关注

    When you use for range, each loop iteration reads from the channel, and you're not using the read value. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能