Go的defer处理


package main

import "fmt"

func deferFunc() (b int) {
    b = 1
    a := true
    defer func() {
        b = 2
    }()

    return
}

func main() {
    fmt.Println(deferFunc())
    // 这里打印出来的是1,而不是2
    // https://stackoverflow.com/questions/37248898/how-does-defer-and-named-return-value-work-in-golang
}

1. TODO: go的defer底层机制

results matching ""

    No results matching ""