When I return (A map slice) from GetCompanyFilingListRes
and print the output my code runs fine for one iteration. But on the second iteration I get a panic: runtime error: invalid memory address or nil pointer dereference
.
package main
import (
"data/edgar"
"fmt"
"net/http"
)
func main() {
url := "calltomylocalserver.com/page.html"
res, _ := http.Get(url)
for i := 0; i < 100000; i++ {
// GetCompanyFilingListRes comsumes res.Body
// before returning a slice of maps
m := edgar.GetCompanyFilingListRes(res)
fmt.Println(m)
}
}
I'm sure this is something quite simple I'm missing but I don't why res
doesn't seem to be accessible after the first iteration. Is the GC removing it from memory? How would I stop this happening? Thanks.
Edit:
I think my mistake with this question was not including the fact that GetCompanyFilingListRes consumes the res.Body
. And as leaf bebop mentioned it can only be consumed once.
There's a related answer I found here.