Go ---- defer the execution and order return

Go and return in order defer the implementation of

  1. Defer the execution order of a plurality of "LIFO";

  2. defer, return, return to perform the logical value of the three should be: return the first execution, return is responsible for writing the results to the return value; then defer the start performing some finishing touches; and finally the function returns the current carrying value exit.

 

If the function's return value is unknown (without naming the return value), then go language will be the implementation of a similar in the implementation of return creates a temporary variable saved as return value of the action, while well-known function that returns a value, since the return value the variables have been defined when the function defined in the implementation of the return value will return to perform save operation, and the subsequent change defer function return value (but defer although the use of a function is performed after the return defined variables, so the impact on the operations performed defer changes will return to a value of variable

eg1: without a named function return value

main Package 

Import "FMT" 

FUNC main () { 
	fmt.Println ( "return:", Test ()) // sequence between defer and return value is returned first, i = 0, the defer 
} 

FUNC Test () int {// the return value is not named here 
	var I int 
	defer FUNC () { 
		I ++ 
		fmt.Println ( "defer1", I) as the closure // quoted, it determines the current value when the function is executed according to defer the whole context. 2 = I 
	} () 
	defer FUNC () { 
		I ++ 
		fmt.Println ( "defer2", I) as the closure // quoted, it determines the current value when the function is executed according to defer the whole context. . 1 = I 
	} () 
	return I 
} 

test () returns the first i = 0

defer2 performed prior to defer1

The output is:

defer2 1

defer1 2

return: 0

 

 

EG2: with a named function return value:

package main

import "fmt"

func main() {
	fmt.Println("return:", test())
}

func test() (i int) { //返回值命名i 
	defer func() {
		i++
		fmt.Println("defer1", i)
	}()
	defer func() {
		i++
		fmt.Println("defer2", i)
	}()
	return i
}
对外部变量的引用作为函数参数(i),则在defer申明时就把值传递给defer,

 

 The output is:

defer2 1

defer1 2

return: 2

Understand the operating mechanism of the return return value:

In order to clarify the difference between the above two cases, we must first understand the operating mechanism of return return value:
return is not an atomic operation, is divided into an assignment, and return values two steps
eg1: in fact return to perform a two-step operation, because the return value not named, so
return to specifying a return value (assumed to be s), is first assigned to s i, a subsequent
operation is as for i, carried out, it will not affect the s, s are not updated thereafter because, so
return s does not change
corresponding to:
var I int
s: = I
return s
EG2: Ibid, s is equivalent to the named variable i, because all operations are based on
the named variable i (s), is the return value of i, so every a defer operation, updates the
return value i

 

 

Guess you like

Origin www.cnblogs.com/saryli/p/11371912.html