返回值赋值RET指令
  1. 返回值 = x
  2. 执行defer
  3. 执行SET指令
多个defer
后defer先执行
func main()  {
    out()
}

func out() () {
    defer func() {fmt.Println("world")}()
    defer func() {fmt.Println("hello")}()
}

输入

hello
world

Process finished with the exit code 0
带返回值
不影响return返回值
func main()  {
    fmt.Println("return out put", out())
}

func out() int {
    i := 0
    defer func() {
        i++                        // 不影响return返回值
        fmt.Println("defer out put ", i)
    }()
    return i                       // 返回值=i=0; i++
}

输入

defer out put  1
return out put 0
带命名返回值

defer影响return返回值

func main()  {
    fmt.Println("return out put", out())
}

func out() (i int) {
    defer func() {
        i++                            // 影响return返回值
        fmt.Println("defer out put ", i)
    }()
    return 1                           // i=1; i++
}

输入

defer out put  2
return out put 2