1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| // https://stackoverflow.com/questions/33426977/how-to-golang-check-a-variable-is-nil
package main
import (
"bytes"
"net/http"
)
func main() {
client := &http.Client{}
var content *bytes.Reader
content = nil
// it's error
// req, _ := http.NewRequest("GET", "http://www.github.com", content)
// it's ok
req, _ := http.NewRequest("GET", "http://www.github.com", nil)
resp, _ := client.Do(req)
defer resp.Body.Close()
}
// A go interface consists of a type and a value. An interface is only nil if both the type and the value are nil. You provided a type but no value: Therefore NewRequest tried to call Read on a nil struct (the value of the interface).
|