According to the docs for func (*Client) Do
:
"An error is returned if caused by client policy (such as CheckRedirect), or if there was an HTTP protocol error. A non-2xx response doesn't cause an error.
When err is nil, resp always contains a non-nil resp.Body."
Then looking at this code:
res, err := client.Do(req)
defer res.Body.Close()
if err != nil {
return nil, err
}
I'm guessing that err
is not nil
. You're accessing the .Close()
method on res.Body
before you check for the err
.
The defer
only defers the function call. The field and method are accessed immediately.
So instead, try checking the error immediately.
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
The nil pointer dereference is in line 65 which is the defer in
res, err := client.Do(req)
defer res.Body.Close()
if err != nil {
return nil, err
}
If err!= nil then res==nil and res.Body panics. Handle err before defering the res.Body.Close().
Since I got here with my problem I will add this answer although it is not exactly relevant to the original question. When you are implementing an interface make sure you do not forget to add the type pointer on your member function declarations. Example:
type AnimalSounder interface {
MakeNoise()
}
type Dog struct {
Name string
mean bool
BarkStrength int
}
func (dog *Dog) MakeNoise() {
//implementation
}
I forgot the (dog *Dog) part, I do not recommend it. Then you get into ugly trouble when calling MakeNoise
on an AnimalSounder
interface variable of type Dog
.
I know this might be a coding question, but for others who were looking for another answer, the issue was that I have the program running in powershell and closed powershell without killing the process. The process was using the same port so it was failing with the same error above. I have to manually kill the process and then it worked fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With