package main import ( "fmt" ) func calc(index string, a, b int) int { ret := a + b fmt.Println(index, a, b, ret) return ret } func main() { a := 1 b := 2 defer calc("1", a, calc("10", a, b)) a = 0 defer calc("2", a, calc("20", a, b)) b = 1 } /* 10 1 2 3 20 0 2 2 2 0 2 2 1 1 3 4 */
Test sites: defer the execution order answer: to note defer the execution order and deliver value index: 1 is definitely the last to perform, but the index: 1. The third parameter is a function, so the first is called calc ( "10", 1 2) ==> 10,1,2,3 execute index: 2, as before, we need to call the calc ( "20", 0,2) ==> 20,0,2,2 to execute b = 1 time to start calling, index: 2 ==> calc ( "2", 0,2) ==> 2,0,2,2 final performance index: 1 ==> calc ( " 1", 1,3) = => 1,1,3,4