I have the following code. Pay special attention to the anonymous function:
func saveMatterNodes(matterId int, nodes []doculaw.LitigationNode) (bool, error) {
var (
err error
resp *http.Response
)
// Do this in multiple threads
for _, node := range nodes {
fmt.Println("in loops")
go func() {
postValues := doculaw.LitigationNode{
Name: node.Name,
Description: node.Description,
Days: node.Days,
Date: node.Date,
IsFinalStep: false,
Completed: false,
Matter: matterId}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(postValues)
resp, err = http.Post("http://127.0.0.1:8001/matterNode/", "application/json", b)
io.Copy(os.Stdout, resp.Body)
fmt.Println("Respone from http post", resp)
if err != nil {
fmt.Println(err)
}
}()
}
if err != nil {
return false, err
} else {
return true, nil
}
}
If I remove the go func() {}()
part and just leave the code in between it seems to execute fine but the moment I add it back it does not execute. Any idea why that is? I initially thought maybe because it's executing on a different thread but this doesn't seem to be the case as I can see on my webservice access logs that it is not executing.