GO basis of the delay execution

First, what delay?
• That delay (defer) statement, delay statement is used to perform a function call, prior to this function, delayed statement returns.


A delay function

1, you can add multiple defer statement in the function.
• When a function is executed in the end, these statements will defer ■ reverse order execution, and finally the function returns. Especially when some of the open operating resources, the need to return early encounters an error, you need to turn off before returning to the appropriate resources, or is likely to cause resource leaks and other problems you're doing
• If you have a lot of calls defer, defer it is the use of backward first-out mode
• when Jian from where the method of execution (error when will execute)

package main

import "fmt"
import (
    "base"
)
func main() {
    defer funcA()
    funcB ()
    defer funcC()
    fmt.Println("main over...")
}

function func () {
    fmt.Println ( " This is funcA " )
}

funcB function () {
    fmt.Println ( " This is funcB " )
}

funcC function () {
    fmt.Println ( " This is funcC " )
}
View Code

Delaying (similar to a stack data structure defe)

package main

import "fmt"

type person struct {
    firstName, lastName string
}

func (p person) fullName() {
    fmt.Printf("%s %s", p.firstName, p.lastName)
}

func main() {
    p := person{"Steven" , "Wang"}
    defer p.fullName()
    fmt.Print("Welcome ")
}
View Code

defer function with parameters

package main

import "fmt"

func main() {
    a := 5
    b := 6
    defer printAdd(a, b, true)
    a = 10
    b = 7
    printAdd(a, b, false)
}

func printAdd(a, b int, flag bool) {
    if flag {
        fmt.Printf ( " deferred execution function printAdd (), parameters a, b respectively,% d,% d, the sum of two numbers:% D \ n- " , a, b, A + B)
    } else {
        fmt.Printf ( " non-delayed execution function printAdd (), parameters a, b respectively,% d,% d, the sum of two numbers:% D \ n- " , a, b, A + B)
    }
}
View Code

Second, downtime and downtime recovery recover panic


(-), panic and recover mechanism
1. Overview:
• panic: Panic meaning __ recover: "restore ■ _
• Go no exception mechanism like that of Java, it does not throw an exception, but the use of panic and recover mechanism. Always remember that it should be used as a last resort, that is to say, our code should be no, or very few such thing as panic.
• Go language use panicO, recover (), implement the program in a very special exception handling
〇panicO, let the current program into the panic, interrupt program execution
〇recoverO, and return the program to be executed defer function
〇Panics- a built-in function, can interrupt the original control flow into a way to make people panic flow.
square when the function F calls panic, execution of the function F is interrupted, but the delay in F function executes normally, then F returns to the place that called it in place calls, F behaves like call the panic. this process continues up until the occurrence of panic in all goroutine function call returns, this time the program exits.
square panic can directly call the panic generated also be A run-time error, such as cross-border access to an array.
〇Recover is a built-in function that lets enter panicking in goroutine recover.
〇recover valid only delay function. In the normal course of implementation, call to recover will return nil, and no other effect. If the current goroutine panic, calling recover • can capture the input value panic, and resume normal execution

 

package main

import "fmt"

func main() {
    func ()
    funcB ()
    funcC ()
    fmt.Println("main over")
}

function func () {
    fmt.Println ( " This is funcA " )
}

funcB function () {
    defer func() {
        if msg := recover(); msg != nil {
            fmt.Println ( " restore you, get recover the return value: " , msg)
        }

    }()
    fmt.Println("这是funcB")
    for i := 0; i < 10; i++ {
        fmt.Println("i:", i)
        if i == 5 {
            //panic("funcB恐慌啦")
        }
    }
}

funcC function () {
    defer func() {
        fmt.Println ( " execution delayed function " )
        msg := recover()
        fmt.Println ( " get recover the return value: " , msg)
    }()
    fmt.Println ( " This is funcC " )
    panic ( " funcC panicked " )
}
View Code

 

Guess you like

Origin www.cnblogs.com/jalja/p/11846027.html