当前位置:网站首页>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())
}

边栏推荐
- [STL source code analysis] container (to be supplemented)
- ESP32-C3入门教程 基础篇⑫——量产烧写设备配置和序列号, NVS partition分区确认, NVS 分区生成程序, csv转bin
- 煥發青春的戴爾和蘋果夾擊,兩大老牌PC企業極速衰敗
- LVGL 8.2 Image styling and offset
- iptables目标TPROXY
- 线代(高斯消元法、线性基)
- Rejuvenated Dell and apple hit each other, and the two old PC enterprises declined rapidly
- 语音识别-基础(一):简介【语音转文本】
- SQL必需掌握的100个重要知识点:创建和操纵表
- LiveData源码赏析三 —— 常见问题
猜你喜欢

Deep dive kotlin synergy (18): hot and cold data flow

The intelligent DNA molecular nano robot model is coming

Deep dive kotlin synergy (16): Channel

Cp2112 teaching example of using USB to IIC communication

sCrypt 中的 ECDSA 签名验证
![[机缘参悟-34]:光锥之内皆命运](/img/3e/9f5630ba382df7f7ce00705445cef8.jpg)
[机缘参悟-34]:光锥之内皆命运

List introduction

ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528

The first China Digital Collection conference will be held soon

pytorch 笔记 torch.nn.BatchNorm1d
随机推荐
WireGuard简单配置
ESP32-C3入门教程 IoT篇⑤——阿里云 物联网平台 EspAliYun RGB LED 实战之批量生产的解决方案
LVGL 8.2 Checkboxes as radio buttons
Pytorch notes: validation, model eval V.S torch. no_ grad
pytorch 笔记:validation ,model.eval V.S torch.no_grad
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
LVGL 8.2图片缩放及旋转
SQL必需掌握的100个重要知识点:联结表
Mysql database foundation: TCL transaction control language
Wireguard simple configuration
蚂蚁金服笔试题:需求文档有什么可以量化的【杭州多测师】【杭州多测师_王sir】...
8行代码实现快速排序,简单易懂图解!
SQL必需掌握的100个重要知识点:使用存储过程
Dell et Apple, deux entreprises de PC établies, se sont effondrées rapidement
datax json说明
CP2112使用USB转IIC通信教学示例
[rust weekly database] num bigint - large integer
List introduction
经典面试题:负责的模块,针对这些功能点你是怎么设计测试用例的?【杭州多测师】【杭州多测师_王sir】...
林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作