6.1. Error
Go builtin language used as an error packet the error Interface Type
Go language errors as a method / function's return value
Custom Error type
//Learn_Go/main.go Package main Import ( "errors" "FMT" ) FUNC Demo (I, K int) (R & lt int, E error) { IF 0 {K == E = errors.New ( "divisor can not be 0 ") return } R & lt = I / K return } FUNC main () { // Result, error: Demo = (6,3) Result, E: Demo = (6,0) ! = nil IF E { fmt.Println ( "execution error, the error message is:", e) // execution error, error information: the divisor is not 0 return } fmt.Println ( "successfully executed, result:", result) // executed successfully results: }
6.2.defer
Go language defer delay function can be completed, the current function execution defer function after completion
The most common is to defer closing the connection (databases, files, etc.), can be followed defer open after the connection is closed
(1) Go language defer no matter where are written the last execution, do not try to turn the code written in the last close
//Learn_Go/main.go Package main Import "FMT" FUNC main () { fmt.Println ( "open connection") // the defer fmt.Println ( "close connection") the defer FUNC () { fmt.Println ( "Close connector ") // the defer execution } () fmt.Println (" operation ") } // results open the connection operates to close the connection
(2) a plurality defer
Defer using multiple stack architecture are performed to produce the first
In a lot of code structure may appear to produce multiple objects, and the program hope that these objects flashback closed, more than just defer to solve this problem
//Learn_Go/main.go Package main Import "FMT" FUNC main () { fmt.Println ( "open connection A") the defer fmt.Println ( "close the connection A") fmt.Println ( "open connection B") the defer fmt.Println ( "close connection B") fmt.Println ( "open connection C") the defer fmt.Println ( "close connection C") fmt.Println ( "operation") } // results open the connection A to open the connection B C to open the connection to operate close connection C close connection B to close the connection A
(3) defer binding and return
When the return defer exist, should be understood to return two binding executed, a return instruction is to give value
Assignment, another return jump instruction function
The overall implementation of the order and return defer
- Give return value assignment
- Defer execution
- Return out of function
(4) the return value is not defined to receive variables, return values have been assigned when executed defer
//Learn_Go/main.go package main import "fmt" func demo() int { i := 1 defer func() { i = i + 2 }() return i } func main() { fmt.Println(demo()) //1 }
(5) receiving the return value of the variable declaration, the modified contents of the return value, when executed defer
//Learn_Go/main.go package main import "fmt" func demo() (z int) { i := 1 defer func() { z = i + 2 }() return } func main() { fmt.Println(demo()) //3 }
6.3.panic
panic function is build in, when performing the panic, the remaining code execution terminates, and prints an error message stack.
//Learn_Go/main.go Package main Import "FMT" FUNC main () { fmt.Println ( "111") panic ( "error") fmt.Println ( "222") } // Results 111 panic: Error message . 1 goroutines [running]: main.main () C: /Users/86158/Desktop/Learn_Go/main.go:. 8 + 0x82
panic not immediately stop the program, defer or executed
//Learn_Go/main.go Package main Import "FMT" FUNC main () { defer fmt.Println ( "defer the execution contents") fmt.Println ( "111") panic ( "error") fmt.Println ( " 222 ") } // results 111 performed defer content panic: error message goroutines. 1 [running]: main.main () C: /Users/86158/Desktop/Learn_Go/main.go: 0xDC. 9 +
6.4.recover
recover () indicate panic reply program (), so that normal program execution
rcover () is builtin and panic are the same as in function, can receive information panic and restore normal program execution
Recover () is generally within the defer, if not panic information returns nil; if panic, panic state will cancel Recover
//Learn_Go/main.go Package main Import "FMT" FUNC main () { the defer FUNC () { IF error: = Recover (); error = nil {! Fmt.Println ( "panic as:", error) } } () fmt.Println ( "111") panic ( "error occurred") fmt.Println ( "222") } // results 111 panic as: message error has occurred
During the function call panic and recover ()
- recover () function can only restore the current level or function of the current function call panic (), calls the end of the current level of function after recovery, but call the function this function can continue
- panic would have been passed up, if not recover () indicates that the program is terminated, but met recover (), recover () function represents a level where there is no panic, panic would not have passed up
//Learn_Go/main.go Package main Import "FMT" FUNC the demo1 () { fmt.Println ( "upper half the demo1") demo2 () fmt.Println ( "lower half of the demo1") } FUNC demo2 () { FMT .Println ( "upper half demo2") Demo3 () fmt.Println ( "demo2 lower half") } FUNC Demo3 () { fmt.Println ( "upper half Demo3") panic ( "panic Demo3 occurred") fmt.Println ( "demo3 lower half") } FUNC main () { fmt.Println ( "program start") demo1 () fmt.Println ( "end of program") } // results program starts demo1 upper half demo2 on half demo3 upper part panic:panic demo3 appear
demo3 add recover ()
//Learn_Go/main.go Package main Import "FMT" FUNC the demo1 () { fmt.Println ( "upper half the demo1") demo2 () fmt.Println ( "lower half of the demo1") } FUNC demo2 () { FMT .Println ( "upper half demo2") Demo3 () fmt.Println ( "demo2 lower half") } FUNC Demo3 () { the defer FUNC () { Recover () } () half portion fmt.Println ( "demo3 ") panic (" panic demo3 appears in ") fmt.Println (" Demo3 lower half ") } FUNC main () { fmt.Println (" program start ") the demo1 () fmt.Println (" end of program ") } // Results program starts on demo1 half half portion demo2 upper part demo3 demo2 lower half demo1 lower half of the program ended
demo2 add recover ()
//Learn_Go/main.go Package main Import "FMT" FUNC the demo1 () { fmt.Println ( "upper half the demo1") demo2 () fmt.Println ( "lower half of the demo1") } FUNC demo2 () { the defer FUNC () { Recover () } () fmt.Println ( "upper half demo2") Demo3 () fmt.Println ( "demo2 lower half") } FUNC Demo3 () { fmt.Println (upper part demo3 " ") panic (" panic demo3 appears in ") fmt.Println (" Demo3 lower half ") } FUNC main () { fmt.Println (" program start ") the demo1 () fmt.Println (" end of program ") } // Results program starts demo1 upper half of the upper half demo2 upper part demo3 demo1 lower half of the program ended