Study and sort out one Golang knowledge point every time, and make a little progress every day. Today I want to reinforce the points mentioned in question 19 :defer and the return value 🤔
My body, province
- Write the output of the following code snippet.
func f1(a) (result int) {
defer func(a) {
result++
}()
return 0
}
func f2(a) (result int) {
t := 5
defer func(a) {
t = t + 5} ()return t
}
func f3(a) (r int) {
defer func(r int) {
r = r + 5
}(r)
return 2
}
func main(a) {
fmt.Println(f1())
fmt.Println(f2())
fmt.Println(f3())
}
Copy the code
Look at the answers below…
The answer
Reference answer:
1. The output result of the program is: 1 5 2
In Question 19, we mentioned how defer behaves differently in the anonymous and named return value functions; It is found that some students still do not fully understand. Although these are named functions, they can also apply the formula. Let’s write down the disassembly formula again and analyze the disassembly one by one.
In the case of an anonymous return function, the final return process can be broken down into the following steps:
- NValue := result (nValue);
- Then check to see if the defer function exists and execute it if so
- Returns the return value just created :nValue
In the case of a named return function, the return value is already defined when the method is defined, so you do not need to perform the assignment procedure described in step 1. Let’s disassemble the above problems by referring to the formula below:
f1():
func f1(a) (result int) {
defer func(a) {
result++
}()
return 0
}
Copy the code
F1 () after disassembly:
func f1New(a) (result int) {
result = 0 The result variable was defined at the time the function was created and assigned a value of 0
defer func(a) { // defer has an anonymous function call, where the value passed is equivalent to copying the function pointer and the result value is changed to 1
result++
}()
return result // Return result with the value 1
}
Copy the code
f2():
func f2(a) (result int) {
t := 5
defer func(a) {
t = t + 5} ()return t
}
Copy the code
F2 () Disassembled:
func f2New(a) (result int) {
t := 5
result = t The result variable was defined at the time the function was created and assigned a value of 5
defer func(a) {
t = t + 5 // No modification of the return value result is involved} ()return result // result is the same as the original 5
}
Copy the code
f3():
func f3(a) (r int) {
defer func(r int) {
r = r + 5
}(r)
return 2
}
Copy the code
F3 () after disassembly:
func f3New(a) (r int) {
r = 2 The r variable was defined at the time the function was created and assigned a value of 2
defer func(r int) {
r = r + 5 There are anonymous function calls in // defer, but r is passed as a value, which means you are modifying a copy of R and the actual return value r has not been modified
}(r)
return r // the value of r is the same as the original assignment, 2
}
Copy the code
Above, did you do right? 😊
Other relevant answers or supplementary knowledge points, welcome to the comment area message supplement!
The next question
- Which of the following code snippets are syntactically incorrect?
// P structure
type P struct{}func f1(i interface{}){}func f2(i *interface{}){}func main(a) {
m := P{}
n := &s
f1(m) //A
f2(m) //B
f1(n) //C
f2(n) //D
}
Copy the code
The answers and explanations will be provided in the next article. Feel free to post your answers in the comments section. One is better than all, welcome to exchange and learn, mutual progress.
Retweets and comments are welcome. For more original articles, please pay attention to the wechat public number “IYue Love Month” or scan the QR code below: