Ayu*_*lik 6 for-loop channel go buffered
我正在尝试 Go 通道,但遇到一个问题,下面的简单程序不会终止。
本质上我想发出一些异步 HTTP get 请求,然后等待它们全部完成。我正在使用缓冲通道,但我不确定这是惯用的方式。
func GetPrice(quotes chan string) {
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://some/api", nil)
req.Header.Set("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
quotes <- string(body)
}
func main() {
const max = 3
quotes := make(chan string, max)
for i := 0; i < max; i++ {
go GetPrice(quotes)
}
for n := range quotes {
fmt.Printf("\n%s", n)
}
}
Run Code Online (Sandbox Code Playgroud)
程序成功打印 3(最多)项
{"price":"1.00"}
{"price":"2.00"}
{"price":"3.00"}
Run Code Online (Sandbox Code Playgroud)
但随后阻塞并且永远不会退出。
sync.WaitGroup可以在这里用来等待所有 goroutine,然后关闭通道quotes:
func getPrice(quotes chan<- string, onExit func()) {
go func() {
defer onExit()
req, _ := http.NewRequest("GET", "https://some/api", nil)
req.Header.Set("Accept", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
panic(err) // should be handled properly
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
quotes <- string(body)
}()
}
func main() {
const max = 3
var wg sync.WaitGroup
quotes := make(chan string, max)
for i := 0; i < max; i++ {
wg.Add(1)
getPrice(quotes, func() { wg.Done() })
}
go func() {
defer close(quotes)
wg.Wait()
}()
for n := range quotes {
fmt.Printf("\n%s", n)
}
}
Run Code Online (Sandbox Code Playgroud)