当前位置:网站首页>Go language defer
Go language defer
2022-06-30 11:07:00 【wuydsec】
/** * @Author: wyd * @Description:defer * @File: function * @Version: 1.0.0 * @Date: 2022/6/28 16:59 * @Blog: https://wuyandao.blog.csdn.net/ */
package main
import "fmt"
//func sum(x, y int) (ret int) {
// return x + y
//}
//func main() {
// //r := sum(4, 2)
// //fmt.Println(r)
// deferdemo()
//}
//Go In language defer Statement will delay the statement that follows it . stay defer When the belonging function is about to return , Press the deferred statement to defer The definition is executed in reverse order , in other words , Be first defer The statement of is finally executed , Finally being defer The sentence of , First executed .
//defer sentence
//defer Mostly used for resource release before the end of a function ( File handle , Database connection ,socket Connect )
//func deferdemo() {
// fmt.Println("heiheihei")
// defer fmt.Println(" Water... Water ")
// fmt.Println(" Jijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijijiji ")
//}
stay Go In the function of language return Statements are not atomic at the bottom , It is divided into assignment of return value and RET Command two steps . and defer The execution time of the statement is just after the return value assignment operation ,RET Before the command is executed . The details are shown in the following figure :
//defer Interview questions
func f1() int {
x := 5
defer func() {
x++
}()
return x
}
func f2() (x int) {
defer func() {
x++
}()
return 5
}
func f3() (y int) {
x := 5
defer func() {
x++
}()
return x
}
func f4() (x int) {
defer func(x int) {
x++
}(x)
return 5
}
func main() {
fmt.Println(f1())
fmt.Println(f2())
fmt.Println(f3())
fmt.Println(f4())
}

边栏推荐
- 华三交换机清空配置
- Algorithme leetcode 86. Liste des liens séparés
- 时间复杂度与空间复杂度
- SQL必需掌握的100个重要知识点:插入数据
- Dell et Apple, deux entreprises de PC établies, se sont effondrées rapidement
- 在IPhone12的推理延迟仅为1.6 ms!Snap等详析Transformer结构延迟,并用NAS搜出移动设备的高效网络结构...
- Problems and solutions in pyinstall packaging for pychart project
- 同事的接口文档我每次看着就头大,毛病多多。。。
- LVGL 8.2 Drop down in four directions
- SQL必需掌握的100个重要知识点:使用视图
猜你喜欢

在 sCrypt 中实现高效的椭圆曲线点加法和乘法

Pytorch Notebook. Nn. Batchnorm1d

软件测试工程师面试基础题(应届生和测试小菜必备)最基础的面试题

Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)

What is erdma as illustrated by Coptic cartoon?

【IC5000教程】-01-使用daqIDEA图形化debug调试C代码

【STL源码剖析】迭代器

IDEA 又出新神器,一套代码适应多端!

ArrayList与顺序表

语音识别-基础(一):简介【语音转文本】
随机推荐
同事的接口文档我每次看着就头大,毛病多多。。。
LVGL 8.2 Image
华三交换机清空配置
List介绍
File sharing server
科普达人丨漫画图解什么是eRDMA?
软件测试工程师面试基础题(应届生和测试小菜必备)最基础的面试题
datax json说明
Collectors. Tomap application
Time complexity and space complexity
Pytorch Notebook. Nn. Batchnorm1d
SQL必需掌握的100个重要知识点:组合查询
【无标题】
时间复杂度与空间复杂度
promise async和await的方法与使用
Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)
【STL源码剖析】迭代器
Jetpack Compose DropdownMenu跟随手指点击位置显示
Classic interview question: responsible modules, how do you design test cases for these function points? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]
What is erdma as illustrated by Coptic cartoon?