golang the basis of the interview series -defer (a)

Grammar go to learn the language is quite fast, but in the actual process will always encounter this or that wrong, then one by one to solve comprehend, go to the mastery of the minutiae of the language, to become a qualified gopher.


Ado, eat a few chestnuts:


1. The following variable definitions correct?


var a int, b int, c int



2. The following wording is correct?


package main

s := "china"

func main() {

}



3. Please write print value:


s := make([]int, 5)

s = append(s, 1, 2, 3)

fmt.Println(s)



How, it is not to find a Diudiu feeling ah:)



===============================



Today Section (Children's Day too wide to happy to blog ~), defer share a few minor details:


Defer Rule number one: The following statement will trigger panic it?


func main() {
deferCall()
}

func deferCall() {

defer func() {
fmt.Println("打印前")
/*if err := recover(); err != nil {
fmt.Println(err)
}*/

}()


defer func() { fmt.Println("打印中") }()

defer func() { fmt.Println("打印后") }()

panic("触发异常")

}


[A] When the panic in the same defer the goroutine, defer attempts to recover (the comment section), since at this time capture not recover, so the output will be summarized as follows:

a. 

"Trigger abnormal"

"After printing"

"Printing"

"Before printing"


b. 

"After printing"

"Printing"

"Before printing"

"Trigger abnormal"


c. 

"After printing"

"Trigger abnormal"

"Printing"

"Before printing"


Several different appearance than the output, the author's understanding (discussing welcome ~) are printed to stdout the result of competition for the print function at the same time, as the actual execution order of the program are: experiencing panic, to defer the implementation of fmt. after println, re-export panic own error.



Defer Rule number two: When defer is declared, the parameters will be resolved in real time


func calc(index string, a, b int) int {

    ret := a + b

    fmt.Println(index, a, b, ret)

    return ret

}

func main() {

    a := 1

    b := 2

    defer calc("1", a, calc("10", a, b))

    a = 0

    defer calc("2", a, calc("20", a, b))

    b = 1

}


[A] is invoked when defer, a, b will take the current real-time analysis value, and to calculate the value of the inner layer defer calc saved, and then performing the function calc defer layer, so the output is:


10 1 2 3
20 0 2 2
2 0 2 2
1 1 3 4



Defer Rule number three: you can read the famous return value


package main

import "fmt"

func main() {
    v := c()    fmt.Println(v)
}

func c() (i int) {
    defer func() { i++ }()    return 1

}


[A] When the return defer, acquired return value i = 1, for which the defer i ++, it returns the output is 2, instead of 1.




And that 's share regarding defer some point, which is very hard to read easy to overlook the knowledge, in order to better grasp the actual combat, keep moving ~


640?wx_fmt=jpeg


Released 2321 original articles · 98 won praise · views 120 000 +

Guess you like

Origin blog.csdn.net/cpongo2/article/details/98201260