In PHP transfer to Golang, write a preliminary comment on the keyword defer

Defer features:

  • Defer was the first to be pushed, and then the functions after it were pushed. After return or Panic, defer was the last to be pushed according to the principle of “first in, last out” to achieve the goal

  • The parameters to the function defer are evaluated when the defer function is executed, and the values of the variables to the function defer are evaluated at execution time. What is it when you execute it, you plug it in

The code comes up (the three different locations of defer and different functions return different values)

package main

import "fmt"

func main() {
	println(DeferFunc1(1))
	println(DeferFunc2(1))
	println(DeferFunc3(1))
}

func DeferFunc1(i int) (t int) {
	t = i  
	fmt.Println("t1",t);
	defer func() {
		fmt.Println("t3",t);
		t += 3
		fmt.Println("t4",t);
	}()
	fmt.Println("t2",t);
	return
}

func DeferFunc2(i int) int {
	t := i
	fmt.Println("t5",t);
	defer func() {
		fmt.Println("t7",t);
		t += 3
		fmt.Println("t8",t);
	}()
	fmt.Println("t6",t);
	return t
}

func DeferFunc3(i int) (t int) {
	fmt.Println("t9",t);
	defer func() {
		fmt.Println("t11",t);
		t += i
		fmt.Println("t12",t);
	}()
	fmt.Println("t10",t);
	return2}Copy the code

Let me show you the results

Because the result is long, three functions are printed at once, so horizontal layout makes it look easier

The first function

1. This function is initialized with the name of the return parameter t and the initial value of the return type T is 0 and the scope of t is for the whole function (highlighted).
2. This function is executed when t is assigned a value of 1, so T1 prints out as 1
3. The value of T was still 1 at the time of return, and there is no doubt that the t value received at the t3 position after the return was deferred is 1
4. T +3=4 after t3 position, because! Since t is scoped for the whole function, the value of t is retained after t=4, so the first function returns 4

The second function

1. This function returns only values defined as variables of that type
2. T is initialized to I. Since t is defined inside the function, the scope of t is inside the function
3. The value of t on return is still 1
4. T7 received the value of T as 1, and after t +3=4, T8 also received 4. However, as the scope of t was only within the function, the returned value of T was not replaced by the operation in defer, which was still 1

The third function

For those of you who have looked at the first two, the third one should be easy to understand
1. Is the return value t of the whole function defined in scope
2. Return t with a value of 2
3. Defer received the return value t as 2 and added the value of I as 3
4. Since t is scoped for the whole function, return t with a value of 3

Bottom line: If defer returns the values of some parameters, rather than just closing the database connection and so on, you’ll need to select the scope of the corresponding parameters, and the results will vary widely depending on the scope

(Over. If you have different opinions, welcome civilized exchange, I hope you can give advice.)