In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to solve the problem judged by nil in Go language". In the daily operation, I believe that many people have doubts about how to solve the problem in the nil language. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt that "nil judges how to solve the problem in Go language". Next, please follow the editor to study!
What is nil?
In the Go language, the zero value of Boolean type (initial value) is false, the zero value of numeric type is 0, the zero value of string type is empty string "", and the zero value of pointer, slice, mapping, channel, function, and interface is nil.
Nil has a built-in variable to represent null values, and only pointers, channel, methods, interfaces, map, and slices can be assigned to nil.
Developers with experience in other programming languages may regard nil as the null (NULL) of other languages, but this is not entirely true, because there are many differences between nil in Go and null in other languages.
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
Problem code
The following code is my encapsulation of the http.Post method
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}
And then use it like this:
Var body * bytes.Readerbody = nilresp, err = req.Post (endpoint, nil, body, nil, nil)
At this point, there will be a null pointer error, and finally, after a long troubleshooting, it is found that it is a null pointer error in http.NewRequest:
Error analysis
The underlying implementation of pointers and interfaces has two parts: data and type. When the pointer and interface are explicitly assigned to nil, both data and type are nil, but when a value that type is not nil but data is nil is assigned to the pointer or interface, the result of comparison with nil is false.
Modify the code
Use reflect.ValueOf (body) .IsNil () to determine whether the body is empty:
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} so far The study on "nil determines how to solve the problem in Go language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.