猿问

有条件的 Go 例程/通道

我想让统计例程有条件,以便它只在某些情况下运行,否则会浪费一半的时间。现在我有一个 go 例程充当生产者,通过缓冲通道为两个消费者例程提供数据。有没有办法让统计例程是有条件的,或者我应该遵循更好的模式?在此先感谢您的任何帮助!


func main() {

    options()

    go produce(readCSV(loc))

    go process()

    go statistics() // only on flag

    <-done

}


func produce(entries [][]string) {

    regex, err := regexp.Compile(reg)

    if err != nil {

        log.Error(reg + ", is not a valid regular expression")

    } else {

        for _, each := range entries {

            if regex.MatchString(each[col]) {

                matches <- each

                stats <- each // only on flag

            }

        }

    }

    done <- true

}


func process() {

    for {

        match := <-matches

        if len(match) != 0 {

            // PROCESS

        }

    }

}


func statistics() {

    for {

        stat := <-stats

        if len(stat) != 0 {

            // STATISTICS

        }

    }

}


犯罪嫌疑人X
浏览 182回答 3
3回答

侃侃无极

将此作为条件并没有错:var stats chan []string&nbsp; // Don't initialize stats.func main() {&nbsp; &nbsp; options()&nbsp; &nbsp; go produce(readCSV(loc))&nbsp; &nbsp; go process()&nbsp; &nbsp; if flag {&nbsp; &nbsp; &nbsp; &nbsp; stats = make(chan []string, 1024)&nbsp; &nbsp; &nbsp; &nbsp; go statistics() // only on flag&nbsp; &nbsp; }&nbsp; &nbsp; <-done}func produce(entries [][]string) {&nbsp; &nbsp; regex, err := regexp.Compile(reg)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Error(reg + ", is not a valid regular expression")&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; for _, each := range entries {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if regex.MatchString(each[col]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matches <- each&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if stats != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stats <- each // only on flag&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; close(done)}func process() {&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case match := <-matches:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(match) != 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // PROCESS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; case <-done:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func statistics() {&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case stat := <-stats:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(stat) != 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // STATISTICS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; case <-done:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

富国沪深

如果您从代码中的许多地方更新统计信息,您可能需要添加一些辅助方法。就像是:type stats struct {&nbsp; &nbsp; ch chan []string}func (s *stats) update(a []string) {&nbsp; &nbsp; if s != nil {&nbsp; &nbsp; &nbsp; &nbsp; s.ch <- a&nbsp; &nbsp; }}func (s *stats) start() {&nbsp; &nbsp; if s != nil {&nbsp; &nbsp; &nbsp; &nbsp; s.ch = make(chan []string)&nbsp; &nbsp; &nbsp; &nbsp; go statistics()&nbsp; &nbsp; }}var s *statsif enabled {&nbsp; &nbsp; s = new(stats)}s.start()// later in the codes.update(each)

暮色呼如

也许您正在寻找 flag 包。import "flag"var withStats = flag.Boolean("s", false, "Do statistics")func main() {&nbsp; &nbsp; flag.Parse()&nbsp; &nbsp; ...&nbsp; &nbsp; if *withStats == true {&nbsp; &nbsp; &nbsp; &nbsp; t := statType&nbsp; &nbsp; &nbsp; &nbsp; size := 100&nbsp; &nbsp; &nbsp; &nbsp; stats := make(chan, t, size)&nbsp; &nbsp; &nbsp; &nbsp; go statistics() // only on flag&nbsp; &nbsp; }&nbsp; &nbsp; ...}
随时随地看视频慕课网APP

相关分类

Go
我要回答