当前位置:网站首页>go语言defer
go语言defer
2022-06-30 10:31:00 【wuydsec】
/** * @Author: wyd * @Description:defer * @File: 函数 * @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语言中的defer语句会将其后面跟随的语句进行延迟处理。在defer归属的函数即将返回时,将延迟处理的语句按defer定义的逆序进行执行,也就是说,先被defer的语句最后被执行,最后被defer的语句,最先被执行。
//defer语句
//defer多用于函数结束之前的资源释放(文件句柄,数据库连接,socket连接)
//func deferdemo() {
// fmt.Println("heiheihei")
// defer fmt.Println("水水水水")
// fmt.Println("叽叽叽叽")
//}
在Go语言的函数中return语句在底层并不是原子操作,它分为给返回值赋值和RET指令两步。而defer语句执行的时机就在返回值赋值操作后,RET指令执行前。具体如下图所示:
//defer面试题
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())
}

边栏推荐
- Ant financial's written test question: what can be quantified in the requirements document? [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
- sCrypt 中的 ECDSA 签名验证
- Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...
- Mysql database foundation: views and variables
- Auto SEG loss: automatic loss function design
- LVGL8.2 Simple Checkboxes
- 7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
- pytorch 笔记 torch.nn.BatchNorm1d
- LVGL 8.2 Drop down in four directions
- 焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
猜你喜欢

DQN笔记

Android 开发面试真题进阶版(附答案解析)

Criu enables hot migration

The intelligent DNA molecular nano robot model is coming

Voir le changement technologique à travers la Légion Huawei (5): Smart Park

go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)

The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)

Overview of currency

China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market

pytorch 笔记 torch.nn.BatchNorm1d
随机推荐
基于HAL库的LED驱动库
焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
Cp2112 teaching example of using USB to IIC communication
Pandora IOT development board learning (HAL Library) - Experiment 1 running lantern (RGB) experiment (learning notes)
Matplotlib notes: contour & Contour
Ant financial's written test question: what can be quantified in the requirements document? [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
Pycharm项目使用pyinstalle打包过程中问题及解决方案
Deep dive kotlin synergy (16): Channel
我们公司使用 7 年的这套通用解决方案,打通了几十个系统,稳的一批!
中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
Collectors.toMap应用
DQN笔记
WireGuard简单配置
Auto SEG loss: automatic loss function design
历史上的今天:微软收购 PowerPoint 开发商;SGI 和 MIPS 合并
文件共享服务器
Foresniffer tutorial: extracting data
[STL source code analysis] iterator
js常见问题