Reading multiple return values from goroutine
php Editor Apple Reading multiple return values from goroutine is a common operation in the Go language. Goroutine is a lightweight thread in the Go language that can achieve concurrent execution. In some cases, we need to get the return value from one or more goroutines for further processing. This operation can be achieved by using channels, which are an important mechanism for communication between goroutines. Through channels, we can pass data between goroutines and achieve synchronization and communication between coroutines. In this article, we will detail the tips and considerations on how to read multiple return values from a goroutine.
Question content
I'm trying to write wc(1) in Go, and I'm trying to use goroutines to compute a large number of input files more efficiently. My code works fine, but I'm having trouble implementing a way to summarize statistics for all go routines. How can I pass the function variables nl
, nw
, nc
to main and aggregate them there after all the go routines have finished their work?
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) }
Solution
You are close to the answer! I'd suggest a quick refactor to return a Result
object with numbers, which would allow adding them at the end easily (instead of using a string). Therefore, you can use chan result
instead of chan string
.
Basically, you can introduce a totalResult
variable and when iterating over all results, just add nl
, nc
and nw# The result of ## is added to this total variable.
<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>
37 50 4753 19 106 821 47 255 3806 Total result: 103 411 9380
The above is the detailed content of Reading multiple return values from goroutine. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...
