前言
代碼封裝是百干不厭的事,但有時候封裝會導(dǎo)致一些問題。本文記錄了個人在封裝 http 請求時遇到的一個和 nil 判斷有關(guān)的問題。
nil 是什么
在 Go 語言中,布爾類型的零值(初始值)為 false,數(shù)值類型的零值為 0,字符串類型的零值為空字符串"",而指針、切片、映射、通道、函數(shù)和接口的零值則是 nil。
nil 內(nèi)置的一個變量,用來代表空值,且只有指針、channel、方法、接口、map 和切片可以被賦值為 nil。
有過其他編程語言開發(fā)經(jīng)驗的開發(fā)者也許會把 nil 看作其他語言中的 null(NULL),其實這并不是完全正確的,因為Go語言中的 nil 和其他語言中的 null 有很多不同點。
buildin/buildin.go:
// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type
// Type is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type int
問題代碼
下面的代碼是我對 http.Post 方法的封裝
func (r *Request) Post(endpoint string, params *url.Values, body io.Reader, headers map[string]string, cookies map[string]string) (resp *http.Response, err error) {
url := fmt.Sprintf("%s%s", r.BaseURL, endpoint)
var req *http.Request
req, err = http.NewRequest(http.MethodPost, url, body)
if err != nil {
return
}
r.setRequest(req, params, headers, cookies)
resp, err = r.Client.Do(req)
return
}
然后像下面這樣使用的時候:
var body *bytes.Reader
body = nil
resp, err = req.Post(endpoint, nil, body, nil, nil)
這時會出現(xiàn)空指針的錯誤,最終經(jīng)過漫長的排查發(fā)現(xiàn)是在 http.NewRequest 里出現(xiàn)的空指針錯誤:
錯誤分析
指針和接口的底層實現(xiàn)有兩部分:data 和 type。當(dāng)指針和接口被顯式地賦值為 nil 時,data 和 type 同時為 nil,但是將一個 type 不為 nil 但 data 為 nil 的值賦值給指針或接口時,再與 nil 作比較的結(jié)果則是 false。
修改代碼
使用 reflect.ValueOf(body).IsNil() 判斷 body 是否為空:
func (r *Request) Post(endpoint string, params *url.Values, body io.Reader, headers map[string]string, cookies map[string]string) (resp *http.Response, err error) {
url := fmt.Sprintf("%s%s", r.BaseURL, endpoint)
var req *http.Request
if reflect.ValueOf(body).IsNil() {
req, err = http.NewRequest(http.MethodPost, url, nil)
} else {
req, err = http.NewRequest(http.MethodPost, url, body)
}
if err != nil {
return
}
r.setRequest(req, params, headers, cookies)
resp, err = r.Client.Do(req)
return
}
總結(jié)
到此這篇關(guān)于Go語言中nil判斷引起問題的文章就介紹到這了,更多相關(guān)Go nil判斷問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Django繼承自帶user表并重寫的例子
- 分析Go語言中CSP并發(fā)模型與Goroutine的基本使用
- Go遍歷struct,map,slice的實現(xiàn)
- Go 容器遍歷的實現(xiàn)示例
- Golang的繼承模擬實例