# Go: defer与return小记

#### 1 官方定义

A defer statement pushes a function call onto a list. The list of saved calls is executed after the surrounding function returns. Defer is commonly used to simplify functions that perform various clean-up actions.
defer表达式将一个函数调用保存在列表中，当包裹defer的函数"返回"后，列表中的调用会被执行。defer通常用于清理收尾工作。
`注意：这里的返回加了引号，原因如下`

#### 2 实现逻辑

`参考 雨痕大神的读书笔记(https://github.com/qyuhen/book)源码第20章`

step 1 : 在defer表达式的地方，会调用runtime.deferproc(size int32, fn *funcval)保存延时调用，注意这里保存了延时调用的参数
step 2 : 在return时，先将返回值保存起来
step 3 : 按FILO顺序调用runtime.deferreturn，即延时调用
step 4 : RET指令

#### 3 避坑提示

###### 1. defer的参数在声明时即被确定下来，先看个例子(生产环境这样写估计会被唾沫喷死)
``````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
``````

###### 2. 有名与无名返回值
``````func namedReturn() (r int) {
defer func() {
r++
fmt.Println("defer in namedReturn : r = ", r)
}()

return
}

func unnamedReturn() int {
var r int
defer func() {
r++
fmt.Println("defer in unnamedReturn : r = ", r)
}()
return r
}

func main() {

fmt.Println("namedReturn : r = ", namedReturn())

fmt.Println("unnamedReturn : r = ", unnamedReturn())
}
``````

``````defer in namedReturn : r =  1
namedReturn : r =  1
defer in unnamedReturn : r =  1
unnamedReturn : r =  0
``````

###### 3. 延时参数与有名返回值遮蔽
``````func ShelteredReturn() (r int) {
defer func(r int) {
r++
fmt.Println("defer in ShelteredReturn : r = ", r)
}(r)
return 0
}

func main() {

fmt.Println("ShelteredReturn : r = ", ShelteredReturn())

}
``````

``````defer in ShelteredReturn : r =  1
ShelteredReturn : r =  0
``````

#### 参考文献

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

#### 1 官方定义

A defer statement pushes a function call onto a list. The list of saved calls is executed after the surrounding function returns. Defer is commonly used to simplify functions that perform various clean-up actions.
defer表达式将一个函数调用保存在列表中，当包裹defer的函数"返回"后，列表中的调用会被执行。defer通常用于清理收尾工作。
`注意：这里的返回加了引号，原因如下`

#### 2 实现逻辑

`参考 雨痕大神的读书笔记(https://github.com/qyuhen/book)源码第20章`

step 1 : 在defer表达式的地方，会调用runtime.deferproc(size int32, fn *funcval)保存延时调用，注意这里保存了延时调用的参数
step 2 : 在return时，先将返回值保存起来
step 3 : 按FILO顺序调用runtime.deferreturn，即延时调用
step 4 : RET指令

#### 3 避坑提示

###### 1. defer的参数在声明时即被确定下来，先看个例子(生产环境这样写估计会被唾沫喷死)
``````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
``````

###### 2. 有名与无名返回值
``````func namedReturn() (r int) {
defer func() {
r++
fmt.Println("defer in namedReturn : r = ", r)
}()

return
}

func unnamedReturn() int {
var r int
defer func() {
r++
fmt.Println("defer in unnamedReturn : r = ", r)
}()
return r
}

func main() {

fmt.Println("namedReturn : r = ", namedReturn())

fmt.Println("unnamedReturn : r = ", unnamedReturn())
}
``````

``````defer in namedReturn : r =  1
namedReturn : r =  1
defer in unnamedReturn : r =  1
unnamedReturn : r =  0
``````

###### 3. 延时参数与有名返回值遮蔽
``````func ShelteredReturn() (r int) {
defer func(r int) {
r++
fmt.Println("defer in ShelteredReturn : r = ", r)
}(r)
return 0
}

func main() {

fmt.Println("ShelteredReturn : r = ", ShelteredReturn())

}
``````

``````defer in ShelteredReturn : r =  1
ShelteredReturn : r =  0
``````