mã của tôi là như thế này, khi tôi sử dụng req, _ := http.NewRequest("GET", "http://www.github.com", content)
, nó sẽ phát ra ngoại lệ:cách golang kiểm tra một biến là con số không
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0xffffffff addr=0x0 pc=0xaab78]
goroutine 1 [running]:
net/http.NewRequest(0x34f3b8, 0x3, 0x378020, 0x15, 0xfeec4350, 0x0, 0x10738801, 0x0, 0x0, 0x107000e0)
/usr/local/go/src/net/http/request.go:570 +0x498
main.main()
/tmp/sandbox056954284/main.go:17 +0xe0
nhưng khi tôi sử dụng req, _ := http.NewRequest("GET", "http://www.github.com", nil)
, nó hoạt động, tại sao? làm thế nào tôi thiết lập giá trị số thứ ba
package main
import (
"bytes"
"net/http"
)
func main() {
client := &http.Client{}
var content *bytes.Reader
content = nil
req, _ := http.NewRequest("GET", "http://www.github.com", content)
resp, _ := client.Do(req)
defer resp.Body.Close()
}
Xem https://golang.org/doc/faq#nil_error. –