In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to avoid panic caused by null pointer in Golang language. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
01. Introduction
In Golang language project development, improper operation of variables will trigger the null pointer to trigger the program panic. A null pointer is a variable of the pointer type of unallocated memory, and the value of the variable is nil, because operating a null pointer will cause panic, so we should be very careful in program development.
02. Return value of structure pointer type
When you call a function or method that returns a value of the structure pointer type, and you need to manipulate the field or method that returns the value, we need to pay attention to triggering the panic raised by the null pointer.
The field of the returned value for the operation:
Func main () {user: = GetUser () fmt.Println (user) fmt.Println (user.Id)} func GetUser () (user * User) {return} type User struct {Id int Name string}
Reading the above code, we get the return value of type * User by calling the function GetUser (), because the return value variable is a null pointer, and when we access the field of the return value, the program throws a panic.
To avoid this kind of null pointer problem, one is to initialize the pointer type variable of the return value at the beginning of the function body in the function or method that the return value contains pointer type variable; the other is to determine whether the return value is nil (null pointer) when you call the function or method of the structure pointer type return value when operating the field or method of the return value.
Func main () {user: = GetUser () fmt.Println (user) if user! = nil {fmt.Println (user.Id)}} func GetUser () (user * User) {user = new (User) / / user = & User {} return} type User struct {Id int Name string}
The method to manipulate the return value:
Func main () {user: = GetUser () user.Login ()} func GetUser () (user * User) {return} type User struct {Id int Name string} func (u User) Login () {}
Reading the above code, we get the return value of type * User by calling the function GetUser (), because the return value variable is a null pointer, and when we access the method Login () of the return value, the program triggers the null pointer to trigger the panic.
To avoid this kind of null pointer problem, first, you can initialize the pointer type variable of the return value in the function body of the function or method whose return value is a pointer type variable; second, the receiver of the type method uses the pointer type.
Func main () {user: = GetUser () user.Login ()} func GetUser () (user * User) {user = new (User) / / user = & User {} return} type User struct {Id int Name string} func (u * User) Login () {} 03, Map of structure pointer type value
In the development of Golang language programs, we often operate the Map of the structure pointer type value, and we should also pay attention to trigger the null pointer to trigger panic.
Func main () {var userData map [int] * User fmt.Println (userData [1] .Name)} type User struct {Id int Name string}
Read the above code, we define the variable of map type userData,key is int type, value is structure pointer type, when we access the value of map, because the value is null pointer, it will throw panic.
To avoid this kind of null pointer problem, we can use ok-idiom mode to determine whether the key value exists. If the key value exists (to determine whether the key value is nil), we access the field of the key value, otherwise we will not. In this way, you can also avoid triggering a null pointer to trigger a panic.
Func main () {var userData map [int] * User if val, ok: = userData [1]; ok {fmt.Println (val.Name)}} type User struct {Id int Name string} 04, defer deferred call
The keyword defer delays the call to the function, although the called function will be delayed, but the variable of the called function will be registered first. So, if the variable of the called function is a null pointer, a panic is thrown.
Func main () {res, err: = http.Get ("http://www.baidu2022.com/robots.txt") / / forge error request defer res.Body.Close () if err! = nil {log.Fatal (err)} body, err: = io.ReadAll (res.Body) if err! = nil {log.Fatal (err)} fmt.Printf ("% s ", body)}
Read the above code and use defer to defer calling the function to release resources, because we put defer after error checking, so if the return value res is a null pointer, panic is thrown.
To avoid this kind of null pointer problem, we can do error checking before using the defer call, and stop execution down after the error is encountered.
On the Golang language how to avoid null pointer caused by the panic to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.