gpt4 book ai didi

go - Goroutines,Channels,WaitGroups,然后选择(只是试图理解)

转载 作者:行者123 更新时间:2023-12-01 21:12:19 24 4
gpt4 key购买 nike

我并不一定要尝试完成某些特定的事情,而只是了解goroutine, channel , WaitGroup 和select(在 channel 上)如何一起使用。我正在编写一个简单的程序,该程序循环遍历URL的一部分,获取URL,然后基本上就结束了。一个简单的想法是,我希望所有获取都发生并返回,通过 channel 发送其数据,然后在所有获取发生后结束。我快到了,而且我知道我的选择中缺少要结束循环的内容,说“嘿, WaitGroup 现在是空的”,但是我不确定如何最好地做到这一点。介意看看并为我清理吗?现在一切正常,只是没有终止,所以很明显我缺少了一些东西,并且/或者不了解这些组件中的某些组件应该如何协同工作。

package main

import (
"fmt"
"io/ioutil"
"net/http"
"sync"
)

var urls = []string{
"https://www.google.com1",
"https://www.gentoo.org",
}

var wg sync.WaitGroup

// simple struct to store fetching
type urlObject struct {
url string
success bool
body string
}

func getPage(url string, channelMain chan urlObject, channelError chan error) {

// increment waitgroup, defer decrementing
wg.Add(1)
defer wg.Done()
fmt.Println("fetching " + url)

// create a urlObject
uO := urlObject{
url: url,
success: false,
}

// get URL
response, getError := http.Get(url)

// close response later on
if response != nil {
defer response.Body.Close()
}

// send error over error channel if one occurs
if getError != nil {
channelError <- getError
return
}

// convert body to []byte
body, conversionError := ioutil.ReadAll(response.Body)
// convert []byte to string
bodyString := string(body)

// if a conversion error happens send it over the error channel
if conversionError != nil {
channelError <- conversionError
} else {
// if not send a urlObject over the main channel
uO.success = true
uO.body = bodyString
channelMain <- uO
}

}

func main() {

var channelMain = make(chan urlObject)
var channelError = make(chan error)

for _, v := range urls {
go getPage(v, channelMain, channelError)
}

// wait on goroutines to finish
wg.Wait()

for {
select {
case uO := <-channelMain:
fmt.Println("completed " + uO.url)
case err := <-channelError:
fmt.Println("error: " + err.Error())
}
}

}

最佳答案

您需要进行以下更改:

  • 正如人们提到的那样,您可能要在调用goroutine之前先在主函数中调用wg.Add(1)。这样,您知道它发生在defer wg.Done()调用之前。
  • 您的 channel 读取将被阻止,除非您能找到一种方法来关闭goroutines中的 channel 或对其进行缓冲。可能最简单的方法是对其进行缓冲,例如var channelMain = make(chan urlObject, len(urls))
  • select语句中的break将仅退出选择,而不退出包含的for循环。您可以标记for循环并中断该循环,或使用某种条件变量。

  • 游乐场链接到工作版本: https://play.golang.org/p/WH1fm2MhP-L
    package main

    import (
    "fmt"
    "io/ioutil"
    "net/http"
    "sync"
    )

    var urls = []string{
    "https://www.google.com1",
    "https://www.gentoo.org",
    }

    var wg sync.WaitGroup

    // simple struct to store fetching
    type urlObject struct {
    url string
    success bool
    body string
    }

    func getPage(url string, channelMain chan urlObject, channelError chan error) {

    // increment waitgroup, defer decrementing
    defer wg.Done()
    fmt.Println("fetching " + url)

    // create a urlObject
    uO := urlObject{
    url: url,
    success: false,
    }

    // get URL
    response, getError := http.Get(url)

    // close response later on
    if response != nil {
    defer response.Body.Close()
    }

    // send error over error channel if one occurs
    if getError != nil {
    channelError <- getError
    return
    }

    // convert body to []byte
    body, conversionError := ioutil.ReadAll(response.Body)
    // convert []byte to string
    bodyString := string(body)

    // if a conversion error happens send it over the error channel
    if conversionError != nil {
    channelError <- conversionError
    } else {
    // if not send a urlObject over the main channel
    uO.success = true
    uO.body = bodyString
    channelMain <- uO
    }

    }

    func main() {

    var channelMain = make(chan urlObject, len(urls))
    var channelError = make(chan error, len(urls))

    for _, v := range urls {
    wg.Add(1)
    go getPage(v, channelMain, channelError)
    }

    // wait on goroutines to finish
    wg.Wait()

    for done := false; !done; {
    select {
    case uO := <-channelMain:
    fmt.Println("completed " + uO.url)
    case err := <-channelError:
    fmt.Println("error: " + err.Error())
    default:
    done = true
    }
    }

    }

    关于go - Goroutines,Channels,WaitGroups,然后选择(只是试图理解),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64027745/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com