前言
代碼封裝是百干不厭的事,但有時(shí)候封裝會(huì)導(dǎo)致一些問(wèn)題。本文記錄了個(gè)人在封裝 http 請(qǐng)求時(shí)遇到的一個(gè)和 nil 判斷有關(guān)的問(wèn)題。
nil 是什么
在 Go 語(yǔ)言中,布爾類(lèi)型的零值(初始值)為 false,數(shù)值類(lèi)型的零值為 0,字符串類(lèi)型的零值為空字符串"",而指針、切片、映射、通道、函數(shù)和接口的零值則是 nil。
nil 內(nèi)置的一個(gè)變量,用來(lái)代表空值,且只有指針、channel、方法、接口、map 和切片可以被賦值為 nil。
有過(guò)其他編程語(yǔ)言開(kāi)發(fā)經(jīng)驗(yàn)的開(kāi)發(fā)者也許會(huì)把 nil 看作其他語(yǔ)言中的 null(NULL),其實(shí)這并不是完全正確的,因?yàn)镚o語(yǔ)言中的 nil 和其他語(yǔ)言中的 null 有很多不同點(diǎn)。
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
問(wèn)題代碼
下面的代碼是我對(duì) 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
}
然后像下面這樣使用的時(shí)候:
var body *bytes.Reader
body = nil
resp, err = req.Post(endpoint, nil, body, nil, nil)
這時(shí)會(huì)出現(xiàn)空指針的錯(cuò)誤,最終經(jīng)過(guò)漫長(zhǎng)的排查發(fā)現(xiàn)是在 http.NewRequest 里出現(xiàn)的空指針錯(cuò)誤:
錯(cuò)誤分析
指針和接口的底層實(shí)現(xiàn)有兩部分:data 和 type。當(dāng)指針和接口被顯式地賦值為 nil 時(shí),data 和 type 同時(shí)為 nil,但是將一個(gè) type 不為 nil 但 data 為 nil 的值賦值給指針或接口時(shí),再與 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語(yǔ)言中nil判斷引起問(wèn)題的文章就介紹到這了,更多相關(guān)Go nil判斷問(wèn)題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Django繼承自帶user表并重寫(xiě)的例子
- 分析Go語(yǔ)言中CSP并發(fā)模型與Goroutine的基本使用
- Go遍歷struct,map,slice的實(shí)現(xiàn)
- Go 容器遍歷的實(shí)現(xiàn)示例
- Golang的繼承模擬實(shí)例