繁体   English   中英

对于范围与静态通道长度golang

[英]for range vs static channel length golang

我有一个通道,它从一个日志文件中解析事件,而另一个则用于同步。 为了我的测试,有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
}

我必须丢失一些与通道上的for循环有关的东西。

我尝试添加time.Sleepdone <- true time.Sleep部分之前。 它并没有改变结果。

for range ,每个循环迭代都从通道读取,并且您没有使用读取值。 因此,将丢弃一半的值。 它应该是:

for ev := range ch {
        evtList = append(evtList, ev)
        count++
}

为了实际利用循环迭代器中读取的值。

围棋范围已在“ 围棋旅”中进行了演示,并在围棋规范中进行了详细说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM