从goroutines阅读多个返回值

k2arahey  于 10个月前  发布在  Go
关注(0)|答案(2)|浏览(60)

我正在尝试用Go语言编写wc(1),并且尝试使用goroutines来更有效地计算大量输入文件。我的代码运行得很好,但是我很难实现一个方法来总结所有go例程的统计数据。如何将函数变量nlnwnc传递给main,并在所有go例程完成工作后在那里汇总它们?

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    ch := make(chan string)

    for _, arg := range os.Args[1:] {
        go wc(arg, ch)
    }
    for range os.Args[1:] {
        fmt.Println(<-ch)
    }
    // Todo: summarize results...
}

func wc(arg string, ch chan<- string) {
    nl, nw, nc := 0, 0, 0

    file, err := os.Open(arg)
    if err != nil {
        fmt.Println("Can't open file: %v", err)
    }
    defer file.Close()

    scan := bufio.NewScanner(file)
    for scan.Scan() {
        nl++

        line := scan.Text()
        words := bufio.NewScanner(strings.NewReader(line))
        words.Split(bufio.ScanWords)
        for words.Scan() {
            nw++
        }

        runes := bufio.NewReader(strings.NewReader(line))
        for {
            if _, _, err := runes.ReadRune(); err != nil {
                break
            } else {
                nc++
            }
        }
    }

    ch <- fmt.Sprintf("%8d%8d%8d", nl, nw, nc+nl)
}
vmjh9lq9

vmjh9lq91#

你已经接近答案了!我建议快速地返回一个带有数字的Result对象,这样可以很容易地在末尾添加数字(而不是使用字符串)。因此,您可以使用chan Result而不是chan string
基本上,您可以引入一个totalResult变量,当迭代所有结果时,只需将nlncnw的结果添加到该total变量中。

package main

import (
    "fmt"
    "math/rand"
)

// define a struct to hold the result
type Result struct {
    nl int
    nw int
    nc int
}

// this is to be able to use fmt.Println(result)
func (r Result) String() string {
    return fmt.Sprintf("%8d%8d%8d", r.nl, r.nw, r.nc+r.nl)
}

func main() {
    ch := make(chan Result)

    for _, arg := range os.Args[1:] {
        go wc(arg, ch)
    }

    totalResult := Result{}

    for range os.Args[1:] {
        result := <-ch
        fmt.Println(result) // just for debugging

        // sum everything
        totalResult.nl += result.nl
        totalResult.nw += result.nw
        totalResult.nc += result.nc
    }

    fmt.Println("Total result:")
    fmt.Println(totalResult)
}

func wc(arg string, ch chan<- Result) {
    nl, nw, nc := 0, 0, 0

    // your logic to compute nl, nw, nc goes here

    ch <- Result{nl: nl, nw: nw, nc: nc + nl}
}

你应该得到这样的东西(有3个文件):

37      50    4753
      19     106     821
      47     255    3806
Total result:
     103     411    9380
qoefvg9y

qoefvg9y2#

答案非常简单,并不特定于Go或goroutines:当你需要将多个值作为一个单位传递时,使用compound data type
在Go的情况下,在您的特定情况下,最简单的方法是声明struct类型,并使您的通道传输此类型的值。
类型可能如下所示:

type wcResult struct {
  nl int
  nw int
  nc int
}

ch := make(chan wcResult)

相关问题