我如何发出多个请求并获得goroutines的响应

I need to make multiple request in url and get the return and save in a slice of return to work with then later, but it doesn't work my code:

func main() {
requestTestGoRoutine()
log.Println("END")
}

func requestTestGoRoutine() {
    done := make(chan *http.Response)
    defer close(done)
    for _, r := range requests {
        go execute(r, done)
        var p protocol
        resp := <-done
        json.NewDecoder(resp.Body).Decode(&p)
        fmt.Println("protocol:", p)
        protocols = append(protocols, p)

    }
    fmt.Println("protocols:", protocols)
}

func execute(r map[string]interface{}, done chan *http.Response) {
    bodyRequest := new(bytes.Buffer)
    json.NewEncoder(bodyRequest).Encode(r)
    log.Println("Fazendo request...")
    resp, err := requestControlTower(url, bodyRequest)
    if err != nil {
        log.Fatal(err)
    }
    done <- resp
}

my output in terminal:

2018/06/29 16:10:26 Fazendo request...
protocol: {123456 Aprovado}
2018/06/29 16:10:38 Fazendo request...
protocol: {123457 Aprovado}
2018/06/29 16:10:48 Fazendo request...
protocol: {123458 Aprovado}
2018/06/29 16:10:58 Fazendo request...
protocol: {123459 Aprovado}
2018/06/29 16:11:08 Fazendo request...
protocol: {123410 Aprovado}
2018/06/29 16:11:18 Fazendo request...
protocol: {123411 Aprovado}
protocols: [{123456 Aprovado} {123457 Aprovado} {123458 Aprovado} {123459         
Aprovado} {123410 Aprovado} {123411 Aprovado}]
2018/06/29 16:11:29 END

anyone could help me?

The reason your code only processes one request at a time is that you are waiting for a response from your channel in the requests loop:

resp := <-done

I find it much easier to work with waitgroups and mutexes than to work with channels, so I use them in my example:

var protocolsMutex sync.Mutex
var wg             sync.WaitGroup

func main() {
    requestTestGoRoutine()
    log.Println("END")
}

func requestTestGoRoutine() {
    for _, r := range requests {
        wg.Add(1)
        go execute(r)
    }

    wg.Wait()

    fmt.Println("protocols:", protocols)
}

func execute(r map[string]interface{}, done chan *http.Response) {
    defer wg.Done()

    bodyRequest := new(bytes.Buffer)
    json.NewEncoder(bodyRequest).Encode(r)
    resp, _ := requestControlTower(url, bodyRequest)

    var p protocol
    json.NewDecoder(resp.Body).Decode(&p)

    protocolsMutex.Lock()
    log.Println("Fazendo request...")
    protocols = append(protocols, p)
    protocolsMutex.Unlock()
}

Here, for each request in the requests loop in requestTestGoRoutine(), I increment the sync.WaitGroup by one and start a execute goroutine for the request. In the execute function, I run defer wg.Done(), which will de-increment the waitgroup by one once the goroutine returns.

After the requests loop, I run wg.Wait(), which will wait until all goroutines have called wg.Done() before printing the protocols slice.

A sync.Mutex is used to control access to the protocols slice so that only one goroutine can access it at a time. If the mutex is locked, the other goroutines will wait until it is unlocked before continuing to the next section of code. Without the mutex, more than one goroutine could write to the slice at once, causing a race condition. I also moved the log.Println statement into this mutex to prevent more than one goroutine from logging at once, which could cause jumbled lines in your log.

I don't have access to all of your code, so this is untested. If something doesn't doesn't work feel free to let me know in a comment.