Go learn the function from scratch (c): downtime and recovery

Delayed execution defer:

When using certain functions required until the end of the statement, you can use the defer statement

The most common is to avoid using defer when added after opening os.Open Close () end of the program does not close the file

defer statement

 

example:
func main() {
 fmt.Println("test")
 for i:=0;i<3 ;i++ {
  fmt.Println(i)
  defer fmt.Println(i)
 }
}

operation result:

test

0

1

2

2

1

0

 

Examples of file operations:
File, err: = os.Open (filename)
  IF ! err = nil {
    return err 
 } 
 / * 
 back here defer to write err in judgment rather than os.Open back 
 if the resources are not succeed, it is not necessary to perform the release of resources operation 
 If err is not nil and release resources operations execution, may lead to panic 
 * / 
 the defer File.close ()

defer statement will be deferred to function after the outer function returned.

Postpone calling a function of its parameters evaluated immediately, but until the outer function returns the function will not be called. Delayed function call is pushed onto a stack (LIFO).

When the function returns the outer layer, the function will be delayed in the order of LIFO calls, may modify the return value to arrive at an incorrect result.

 

Down panic:

When you need to manually trigger downtime in the program, you can use the built-in panic statements

func panic(v interface{})

Rest of the code will not be executed after the panic, and at the same time will stack goroutine information output to the console

main FUNC () { 
 fmt.Println ( " position. 1 " ) 
 the defer fmt.Println ( " the defer location. 1 " ) 
 fmt.Println ( " position 2 " ) 
 panic ( " Manual collapse " ) 
 the defer fmt.Println ( " the defer position 2 " ) 
 fmt.Println ( " position 3 " ) 
}

 

operation result

panic: Manual crash

Position 1

Position 2

defer position 1

goroutine 1 [running]:

main.main ()

D:/SoloWork/Learn/main.go:13 +0x13a

 

Recover recover:

When the program is used to recover the function of panic continues to run

func recover() interface{}

Use method is to defer a statement before the panic closure function, let inside recover capture panic exceptions, such as the face behind unimportant panic can throw an error while still allowing the program to continue to handle the error.

After recover, logic does not go back to the panic that point, the function will still return after defer, that is to say the current function is aborted, but the upper function will continue to run.

example:
main FUNC () { 
 fmt.Println ( " position 1 " ) 
 the defer fmt.Println ( " the defer position 1 " ) 
 fmt.Println ( " position 2 " ) 
 the defer FUNC () { // must first declare defer, or can not capture to panic abnormal 
  fmt.Println ( " the defer entering function " )
   IF ERR:! = Recover (); ERR = nil { 
    fmt.Println (ERR) // ERR here is actually panic incoming content 
  } 
  fmt.Println ( " error handling defer " ) 
 } () 
 panic ( " crash " )
  //Here does not execute down 
 the defer fmt.Println ( " the defer position 2 ' ) 
 fmt.Println ( " position 3 " ) 
}

operation result:

Position 1

Position 2

defer entry to the function

collapse

defer Error Handling

defer position 1

ContinueRun FUNC () { 
 the defer FUNC () { // must first declare defer, or can not capture the panic abnormal 
  fmt.Println ( " the defer entering function " )
   IF ERR: = Recover (); ERR =! nil { 
   fmt.Println (ERR) // ERR here is actually panic incoming content 
  } 
  fmt.Println ( " the defer error handling " ) 
 } () 
 panic ( " crash " ) 
 fmt.Println ( " position 4 " ) 
} 
FUNC main ( ) { 
 fmt.Println ( " position. 1 " ) 
 the defer fmt.Println ( "defer location. 1 " ) 
 fmt.Println ( " position 2 " ) 
 ContinueRun () 
 defer fmt.Println ( " defer position 2 ' ) 
 fmt.Println ( " position 3 " ) 
}

operation result:

Position 1

Position 2

defer entry to the function

collapse

defer Error Handling

Position 3

defer position 2

defer position 1

Guess you like

Origin www.cnblogs.com/VingB2by/p/11117739.html