Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

XDM, I wonder if you have used defer. This syntax is unique to GO, and it is really cool to use

A lot of times, when we are using something new, some inexplicable phenomenon or results, we always think that this thing is not friendly, this thing is very pit, very strange

In fact, we are to understand the attention point, principle, when the so-called strange phenomenon occurs, it will be much easier to deal with

XDM, the following notes are prepared here, please check

Case 1

Firstly, what does defer do?

Is a keyword in GO

This keyword is usually used to free resources and is called before return

If there are multiple defers in the program, the call sequence of defer is based on a stack-like approach of last in first out of LIFO. For details about the implementation principle of defer, see the implementation principle of defer in my history article GO

Let’s take a look at a demo and guess what its output will be.

Write a demo of the input and output sequence of defer and defer together

  • Simply write four functions and apply each to defer
func test1(a) {
	fmt.Println("test 1")}func test2(a) {
	fmt.Println("test 2")}func test3(a) {
	fmt.Println("test 3")}func test4(a) {
	fmt.Println("test 4")}func main(a) {
	defer test1()
	defer test2()
	defer test3()
	defer test4()
}
Copy the code

What do we expect when we run the code above?

test 1
test 2
test 3
test 4
Copy the code

or

test 4
test 3
test 2
test 1
Copy the code

You can run it if you are interested. The second result is defer. We can understand that it is a push operation

Stack: test1(), test2(), test3(), test4()

Stack: test4(), test3(), test2(), test1()

Case 2

Now that we know that defer and defer follow the stack, let’s look at what the order of defer and return looks like.

  • Simply write a function for return and a function for defer
func testDefer(a) {
	fmt.Println("testDefer")}func testReturn(a) int {
	fmt.Println("testReturn")
	return 1
}
func myTest(a) int {
	defer testDefer()
	return testReturn()
}
func main(a) {
	myTest()
}
Copy the code

What about guessing how the above code might be executed

Is that true?

testDefer
testReturn
Copy the code

Or is it like this?

testReturn
testDefer
Copy the code

The result is still the second, as we can see from the above encoding that the statements after defer are executed late and the statements after return are executed first

What if multiple deferred and returns are placed together?

XDM, we can infer from one example, then return will be executed first and defer will be executed according to the stack order

Case 3

Let’s add a simple calculation to this case and see what happens

  • Make it simple to defer with the calculations
func testDefer(num int)(res int){

	defer func(a){
		res = num + 3} ()return num
}

func main(a){

	res := testDefer(5)
	fmt.Println(res)
}
Copy the code

What happens when the above code is run

Is 5? Is eight? I’m sure it’s not three

Think about it, following the logic of case 1 above, the result is 8

Num passed by testDefer and was assigned 5. When num returned, it returned 5. Then execute the defer statement and 5+3 equals 8

Well, that’s enough for today, interested friends can also play

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~