阅读部分不是并发的,但处理是。我这样表述标题是因为我最有可能使用该短语再次搜索这个问题。:)在尝试超越示例之后,我陷入了僵局,因此这对我来说是一次学习经历。我的目标是:逐行读取文件(最终使用缓冲区来处理行组)。将文本传递给func()执行一些正则表达式工作的 a 。将结果发送到某个地方,但要避免互斥或共享变量。我正在向频道发送整数(总是数字 1)。这有点愚蠢,但如果它不会引起问题,我想就这样离开它,除非你们有更简洁的选择。使用工作池来执行此操作。我不确定如何告诉工人重新排队?这是游乐场链接。我试着写一些有用的评论,希望这是有道理的。我的设计可能完全错误,所以不要犹豫重构。package mainimport ( "bufio" "fmt" "regexp" "strings" "sync")func telephoneNumbersInFile(path string) int { file := strings.NewReader(path) var telephone = regexp.MustCompile(`\(\d+\)\s\d+-\d+`) // do I need buffered channels here? jobs := make(chan string) results := make(chan int) // I think we need a wait group, not sure. wg := new(sync.WaitGroup) // start up some workers that will block and wait? for w := 1; w <= 3; w++ { wg.Add(1) go matchTelephoneNumbers(jobs, results, wg, telephone) } // go over a file line by line and queue up a ton of work scanner := bufio.NewScanner(file) for scanner.Scan() { // Later I want to create a buffer of lines, not just line-by-line here ... jobs <- scanner.Text() } close(jobs) wg.Wait() // Add up the results from the results channel. // The rest of this isn't even working so ignore for now. counts := 0 // for v := range results { // counts += v // } return counts}func matchTelephoneNumbers(jobs <-chan string, results chan<- int, wg *sync.WaitGroup, telephone *regexp.Regexp) { // Decreasing internal counter for wait-group as soon as goroutine finishes defer wg.Done() // eventually I want to have a []string channel to work on a chunk of lines not just one line of text for j := range jobs { if telephone.MatchString(j) { results <- 1 } }}func main() { // An artificial input source. Normally this is a file passed on the command line. const input = "Foo\n(555) 123-3456\nBar\nBaz" numberOfTelephoneNumbers := telephoneNumbersInFile(input) fmt.Println(numberOfTelephoneNumbers)}
2 回答
- 2 回答
- 0 关注
- 166 浏览
添加回答
举报
0/150
提交
取消