我正在尝试为http调用创建goroutine。我有大量的URL。
我创建了200个批次,并将一个批次请求发送到api。
问题:-
当我创建客户端对象-client:=&http.Client {}时,我看到创建了一个指针变量,它将为for循环的每次迭代创建一个新的指针变量吗?如何检查创建的客户端对象是否唯一或指向同一对象?
当我以250个批次运行该程序时,我没有看到任何问题,但是一旦增加到270个,它就会失败并显示错误。错误信息如下,不确定是内存错误还是程序错误。
for _, batch := range data_batch {
var str_join string = strings.Join(batch, ",")
// fmt.Println(str_join)
str_join = "{" + "\"inputs\":[" + str_join + "]}"
input_json_data := []byte(str_join)
var output core.Data
go http_call(respond, &wg, input_json_data, output)
}
func http_call(respond chan<- core.Data, wg *sync.WaitGroup, input_json_data []byte, output core.Data) {
defer wg.Done()
// clarifai http request URL
httpurl := os.Getenv("api_url")
//Create the client Object
client := &http.Client{}
//generating the HTTP GET request
request, err := http.NewRequest("POST", httpurl, bytes.NewBuffer(input_json_data))
if err != nil {
log.Println(err)
}
// setting authorization
api_key := "Key " + os.Getenv("api_key")
request.Header.Add("Authorization", api_key)
// setting content-type
request.Header.Add("Content-Type", "application/json")
//calling the URL
response, err := client.Do(request)
if response.StatusCode != 200 {
if err != nil {
log.Println(err)
}
log.Println("statuscode = ", response.StatusCode)
}
bodyBytes, _ := ioutil.ReadAll(response.Body)
bodyString := string(bodyBytes)
err = json.Unmarshal([]byte(bodyString), &output)
if err != nil {
print(err)
}
respond <- output
}
错误:-
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0xb5593]
goroutine 261 [running]:
github.com/p/go/utils.http_call(0xc820440d80, 0xc8204317a0, 0xc82052b730, 0x68, 0x70, 0x0, 0x0, 0x0, 0x0, 0x0, ...)