英文:
How to return multiple values from goroutine?
问题
我找到了一个关于“从 Goroutines 中捕获值”的示例,链接
示例中展示了如何获取值,但如果我想从多个 Goroutines 中返回值,它将无法工作。所以,有人知道如何做吗?
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
)
// WaitGroup 用于等待程序完成 Goroutines。
var wg sync.WaitGroup
func responseSize(url string, nums chan int) {
// 调用 WaitGroup 的 Done 方法告诉 Goroutine 已完成。
defer wg.Done()
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
// 将值发送到无缓冲通道
nums <- len(body)
}
func main() {
nums := make(chan int) // 声明一个无缓冲通道
wg.Add(1)
go responseSize("https://www.golangprograms.com", nums)
go responseSize("https://gobyexample.com/worker-pools", nums)
go responseSize("https://stackoverflow.com/questions/ask", nums)
fmt.Println(<-nums) // 从无缓冲通道中读取值
wg.Wait()
close(nums) // 关闭通道
// >> 永远加载中
}
另外,这个例子是关于worker pools的。
从 fmt.Println(<-results)
这行代码中获取值是不可能的,会导致错误。
英文:
I found some example with "Catch values from Goroutines"-> link
There is show how to fetch value but if I want to return value from several goroutines, it wont work.So, does anybody know, how to do it?
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
)
// WaitGroup is used to wait for the program to finish goroutines.
var wg sync.WaitGroup
func responseSize(url string, nums chan int) {
// Schedule the call to WaitGroup's Done to tell goroutine is completed.
defer wg.Done()
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
// Send value to the unbuffered channel
nums <- len(body)
}
func main() {
nums := make(chan int) // Declare a unbuffered channel
wg.Add(1)
go responseSize("https://www.golangprograms.com", nums)
go responseSize("https://gobyexample.com/worker-pools", nums)
go responseSize("https://stackoverflow.com/questions/ask", nums)
fmt.Println(<-nums) // Read the value from unbuffered channel
wg.Wait()
close(nums) // Closes the channel
// >> loading forever
Also, this example, worker pools
Is it possible to get value from result: fmt.Println(<-results)
<- will be error.
答案1
得分: 3
是的,只需多次从通道中读取即可:
answerOne := <-nums
answerTwo := <-nums
answerThree := <-nums
通道的功能类似于线程安全的队列,允许您将值排队并逐个读取出来。
附注:您应该将等待组增加3次,或者根本不使用等待组。<-nums
将会阻塞,直到 nums 上有一个可用的值,因此不需要等待组。
英文:
Yes, just read from the channel multiple times:
answerOne := <-nums
answerTwo := <-nums
answerThree := <-nums
Channels function like thread-safe queues, allowing you to queue up values and read them out one by one
P.S. You should either add 3 to the wait group or not have one at all. The <-nums will block until a value is available on nums so it is not necessary
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论