首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有条件的Go例程/通道

有条件的Go例程/通道
EN

Stack Overflow用户
提问于 2015-07-08 03:03:47
回答 3查看 849关注 0票数 0

我想使统计例程有条件,以便它只在某些情况下运行,否则它将浪费一半的时间。现在,我有一个去例行公事,作为一个生产者,以满足两个消费者例程通过缓冲渠道。是否有办法使统计例程是有条件的,还是有更好的模式可以遵循?提前感谢您的帮助!

代码语言:javascript
复制
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
        }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-08 03:13:35

有条件地这样做是没有错的:

代码语言:javascript
复制
var stats chan []string  // Don't initialize stats.

func main() {
    options()
    go produce(readCSV(loc))
    go process()
    if flag {
        stats = make(chan []string, 1024)
        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
                if stats != nil {
                    stats <- each // only on flag
                }
            }
        }
    }
    close(done)
}

func process() {
    for {
        select {
        case match := <-matches:
            if len(match) != 0 {
              // PROCESS
            }
        case <-done:
            return
        }
    }
}

func statistics() {
    for {
        select {
        case stat := <-stats:
            if len(stat) != 0 {
                // STATISTICS
            }
        case <-done:
            return
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-07-08 03:14:54

也许你在找旗子包。

代码语言:javascript
复制
import "flag"

var withStats = flag.Boolean("s", false, "Do statistics")

func main() {
    flag.Parse()
    ...
    if *withStats == true {
        t := statType
        size := 100
        stats := make(chan, t, size)
        go statistics() // only on flag
    }
    ...
}
票数 0
EN

Stack Overflow用户

发布于 2015-07-08 03:29:06

如果您更新代码中许多地方的统计信息,您可能需要添加一些助手方法。类似于:

代码语言:javascript
复制
type stats struct {
    ch chan []string
}

func (s *stats) update(a []string) {
    if s != nil {
        s.ch <- a
    }
}

func (s *stats) start() {
    if s != nil {
        s.ch = make(chan []string)
        go statistics()
    }
}

var s *stats
if enabled {
    s = new(stats)
}
s.start()

// later in the code
s.update(each)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31282829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档