SoFunction
Updated on 2025-03-05

Defer keyword in go language

who I am

defer- As the name implies, it is translated as delay, so we usually call itdefer func()sodeferThe function immediately following is the delay function.

Author's note: However, from a practical point of view, delay functions are usually used to do some final work before the function finally returns, so it is actually more appropriate to call it the final function.

Measurements

deferIt has its own unique side. Only by understanding its personality can you start better.

Delay

As the name suggests, since it is called a delay function, it must be delayed.

Let's see what delay method is.

defer_defer.go

// defer_defer.go
package main

import (
	"fmt"
)

func main() {
	foo()
}

func foo() {
	(1)
	defer (2)
	(3)
}

// go run defer_defer.go
// 1
// 3
// 2

You can seedeferThe defined delay function is executed only at the end.

Let’s take another example. If multiple delay functions appear in a function, what is the execution order of the delay functions?

defer_filo.go

// defer_filo.go
package main

import (
	"fmt"
)

func main() {
	foo()
}

func foo() {
	defer (1)
	defer (2)
	defer (3)
}

// go run defer_filo.go
// 3
// 2
// 1

You can see that the delay function defined first is executed, and the delay function defined later is executed first, which complies with the first-in, later-out (FILO) principle of stack.

Influence

Just look at the code,

defer_impact.go

// defer_impact.go
package main

import (
	"fmt"
)

func main() {
	(foo())
}

func foo() (result int) {
	defer func() {
		result++
	}()
	return 0
}

// go run defer_defer.go
// 1

Is the result a little different from the imagination?foo()It can be rewritten as follows:

func foo() (result int) {
	result = 0
	result++
	return
}

in goreturnStatements are not atomic operations.

goreturnThe operation process of the statement is:

  • Set the return value
  • Execute delay function
  • realreturn

Therefore, the delay function will affect the return value of the main function. Of course, it is necessary to distinguish between named return value/anonymous return value, and let's talk about it later.

Deterministic

The parameter value of the delay function is determined when the delay function first appears and is not affected by subsequent operations.

Let's take an example:

defer_parameters.go

// defer_parameters.go
package main

import (
	"fmt"
)

func main() {
	foo()
}

func foo() {
	number := 1
	defer (number)
	number = 2
	return
}

// go run defer_parameters.go
// 1

What can I do

  • Open the data link, remember to close it, you can usedefer
  • After operating the memory resources, remember to release them, you can usedefer
  • If you want to change the name return value of the main function, you can usedefer- Usually it doesn't do that
  • You can use the tricks of being weird and lustful.defer- Occasionally show occasionally, always show always
  • If you want to make trouble, you can use itdefer- Please ensure your own safety of life and property
  • ...

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links