How to prepare two HTTP requests via one client on Go without problems with memory?
I have the next code :
func moduleView(url string) {
var module Module
httpClient := &http.Client{}
moduleId, listHttpResponseCode := findEntity(httpClient, url)
request, _ := http.NewRequest("GET", fmt.Sprintf("%s/api/getById/%s", coreURL, module.ID), nil)
response, _ := httpClient.Do(request)
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
if !statusOK {
fmt.Println("Unable to get by name module: ", response.Status)
} else {
responseBody, _ := ioutil.ReadAll(response.Body)
json.Unmarshal([]byte(strings.TrimSuffix(string(responseBody), "\n")), &module)
fmt.Printf(module)
}
return nil
}
func findEntity(httpClient *http.Client, url string) int {
var module Module
request, _ := http.NewRequest("GET", fmt.Sprintf("%s/api/getByName", url), nil)
response, _ := httpClient.Do(request)
responseBody, _ := ioutil.ReadAll(response.Body)
json.Unmarshal([]byte(strings.TrimSuffix(string(responseBody), "\n")), &module)
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
if !statusOK {
return Module{}, response.StatusCode
}
return module, response.StatusCode
}
Both requests are working, and if use separately no errors. But if trying to use them both together (get ID from one and after do with this data request on another), I'm getting an error with memory
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x1334022]
Please give ideas on how correct to use one httpClient for two requests? Thanks