简体   繁体   English

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

[英]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.Sleepdone <- 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.

相关问题 根据Python列表的长度更改for循环的范围 - Changing the range of a for loop based on the length of a list for Python 使用范围通道中断 for 语句时会发生什么 - What happens when you break the for statement with a range channel 在通道上迭代时出错“范围内的变量太多” - Error "too many variables in range" when iterating over a channel 为什么循环范围内没有增量,其范围是循环的长度,即使长度在循环内增加? - Why there is no increment in the range of a loop, with its range being length of the loop,even though the length gets incremented inside the loop? Enumerable.Range与for循环的性能 - Performance of Enumerable.Range vs for loop python列表变异(for in loop vs range(len)) - python list mutation (for in loop vs range(len)) 获取 bash 中一个范围或一组项目的长度的最简单方法是什么? - What is the simplest way to get the length of a range or set of items in bash? 带有自动说明符和 static_cast 的基于范围的 for 循环 - Range-based for loop with auto specifier combined with static_cast 如何正确使用嵌套的 for 循环来遍历 golang 中的行? - How do I properly use nested for loop to range over rows in golang? 我可以在golang的for-range迭代中使索引为int64吗? - Can I make the index int64 in golang's for-range iteration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM