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 deal with err Interface and defer delay exception in GO language

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

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to deal with err interface and defer delay exceptions in GO language", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "GO language err interface and defer delay exception handling" article.

Err interface

The Go language introduces a standard pattern for error handling, the error interface, which is an interface type built into the Go language. The interface is defined as follows:

Type error interface {Error () string}

Call the corresponding interface

Err:=errors.New ("this is normal err") fmt.Println (err.Error) err2:=fmt.Errorf ("this is normal err") fmt.Println (err2)

Case: when divisor b is 0

Package mainimport ("errors"fmt") func main () {/ / receives error and correct information result,err: = test (5pen0) / / adds judgment If there is no error err=nil if errorformnil {fmt.Println (err)} else {fmt.Println (result)}} / / b is 0, an exception is thrown func test (an int b int) (result int) Err error) {/ / return error message err = nil if bounded messages 0 {err = errors.New ("b cannot be 0")} else {result = a result b} return} panic function

Error returns a generic error, but the panic function returns an error that crashes the program.

Generally speaking, when a panic exception occurs, the program interrupts its operation.

Therefore, we do not directly call the panic () function in the actual development process, but when the program we program encounters a fatal error, the system will automatically call this function to terminate the operation of the whole program, that is, the system has built-in panic function.

Case

Package mainimport "fmt" func main () {test1 () test2 () test3 ()} func test1 () {fmt.Println ("test1")} func test2 () {panic ("panic test2") / / Program interrupt} func test3 () {fmt.Println ("test3")}

Results:

Test1

Panic: panic test2

Goroutine 1 [running]:

Main.test2 (...)

Defer delay

Summary

The keyword defer delays the execution of a function. If it is called, but it is not executed, the parameter will be passed.

Defer fmt.Println ("333") defer fmt.Println ("222") defer fmt.Println (" 111") output order: 111222333

If there are multiple defer statements in a function, they will be executed later in first-out order.

Note that the defer statement can only appear inside the function.

Defer is used in conjunction with anonymous functions

Case 1: no parameters

Package mainimport "fmt" func main () {a: = 10b: = 20 defer func () {fmt.Println ("anonymous function a", a) fmt.Println ("anonymous function b", b)} () a = 100b = 200fmt.Println ("main function a", a) fmt.Println ("main function b") B)}

Results:

Main function a 100

Main function b 200

Anonymous function a 100

Anonymous function b 200

Case 2: with parameters

Package mainimport "fmt" func main () {a: = 10 b: = 20 / / has been called, and parameters have been passed But did not execute defer func (main b int) {/ / add parameter fmt.Println ("anonymous function a", a) fmt.Println ("anonymous function b", b)} (Amaine b) / / pass parameter a = 100b = 200fmt.Println ("main function a", a) fmt.Println ("main function b") B)}

Results:

Main function a 100

Main function b 200

Anonymous function a 10

Anonymous function b 20

Recover prevents program interruption

Once a runtime panic exception is thrown, it can cause the program to crash. This is certainly not what we would like to see, because there is no guarantee that the program will not have any run-time errors.

The Go language provides us with a built-in function specifically designed to "intercept" runtime panic-recover. It can be that the current program recovers from the state of the runtime panic and regains control of the process.

Note: recover is only valid in functions called by defer.

Func testA () {fmt.Println ("testA")} func testB (x int) {/ / set recover () / / use recover () defer func () in the function called by defer {/ / prevent the program from crashing / / recover () / / fmt.Println (recover ()) / / add a layer to judge if err:=recover () Erratic interrupnil {fmt.Println (err)} () / anonymous function var a [3] int a [x] = 999} func testC () {fmt.Println ("testC")} func main () {testA () testB (0) / abnormal interrupt program testC ()} above is about "err interface and defer delay in GO language" What to do with the content of this article I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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