首页  >  文章  >  后端开发  >  从 goroutine 读取多个返回值

从 goroutine 读取多个返回值

PHPz
PHPz转载
2024-02-09 12:30:091054浏览

从 goroutine 读取多个返回值

php小编苹果从goroutine读取多个返回值是Go语言中的一种常见操作。Goroutine是Go语言中的轻量级线程,能够实现并发执行。在某些情况下,我们需要从一个或多个goroutine中获取返回值,以便进一步处理。这种操作可以通过使用通道(channel)来实现,通道是goroutine之间进行通信的重要机制。通过通道,我们可以在goroutine之间传递数据,实现协程之间的同步与通信。在本文中,我们将详细介绍如何从goroutine读取多个返回值的技巧和注意事项。

问题内容

我正在尝试用 Go 编写 wc(1),并且正在尝试使用 goroutine 来更有效地计算大量输入文件。我的代码工作正常,但我很难实现一种方法来总结所有 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)
}

解决方法

您已经接近答案了!我建议快速重构,返回带有数字的 Result 对象,这将允许轻松地在末尾添加它们(而不是使用字符串)。因此,您可以使用 chan 结果 而不是 chan string

基本上,您可以引入一个 totalResult 变量,并且在迭代所有结果时,只需将 nlncnw 的结果添加到该总变量中。

<code>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}
}
</code>

您应该得到类似这样的内容(包含 3 个文件):

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

以上是从 goroutine 读取多个返回值的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除