Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the pattern of Go error handling replacing rr! = nil with panic

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the mode of Go error handling replacing rr! = nil with panic". In daily operation, I believe many people have doubts about how to understand the mode of Go error handling using panic instead of rr! = nil. The editor consulted all kinds of materials and sorted out a simple and useful method of operation. I hope it will be helpful for you to answer the question of "how to understand the mode of Go error handling using panic instead of rr! = nil"! Next, please follow the editor to study!

Why do you want to replace?

In the Go language, if err! = nil is written too much, and it is troublesome and inconvenient to declare various methods:

Err: = foo () if err! = nil {/ / do something.. Return err} err: = foo () if err! = nil {/ / do something.. Return err} err: = foo () if err! = nil {/ / do something.. Return err} err: = foo () if err! = nil {/ / do something.. Return err}

The above is still the sample code, which is more straightforward. If you are in engineering practice, you have to jump all kinds of package around and add if err! = nil, which is more tedious, and you have to care about the upstream and downstream of the whole.

The rest of the more specific will not go into detail, you can follow my official account to read the previous article.

How to replace err! = nil

One of the ways you don't want to write if err! = nil code is to replace it with panic.

The sample code is as follows:

Func GetFish (db * sql.DB, name string) [] string {rows, err: = db.Query ("select name from users where `name` =?", name) if err! = nil {panic (err)} defer rows.Close () var names [] string for rows.Next () {var name string err: = rows.Scan (& name) if err! = nil {panic (err)} names = append (names) Name)} err = rows.Err () if err! = nil {panic (err)} return names}

In the above business code, we replace the function return of return err by panic, so naturally the downstream business code associated with it does not need to write the code of if err! = nil:

Func main () {fish2: = GetFish (db, "fried fish") fish3: = GetFish (db, "salted fish") fish4: = GetFish (db, "fish").}

At the same time, after converting to the error mechanism of using the panic pattern, we must add the recover method to the outer layer:

Func AppRecovery () gin.HandlerFunc {return func (c * gin.Context) {defer func () {if err: = recover (); err! = nil {if _, ok: = err. (AppErr); ok {/ / do something... } else {panic (err)}} ()}}

Each panic is asserted according to the error it throws to identify whether the AppErr error type is customized, and if so, a series of processing actions can be carried out.

Otherwise, you can continue to throw it up the panic to the top-level Recovery method for processing.

This is a relatively complete panic error link handling.

Advantages and disadvantages

In terms of advantages:

The overall code structure looks more concise, focusing only on the implementation logic.

There is no need to focus on and write jumbled error handling code for if err! = nil.

In terms of disadvantages:

With the increase of cognitive burden, every new and old student who takes part in the project is aware of the model and needs to do a basic norm or training.

There is a certain performance overhead, and there is a user-mode context switch in each panic.

There is a certain risk, once the panic does not have recover to live, it will lead to accidents.

Go officially does not recommend it, which runs counter to the definition of panic itself, that is, the conceptual confusion between panic and error.

At this point, the study on "how to understand the mode of Go error handling using panic instead of rr! = nil" 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report