当前位置:网站首页>Defer
Defer
2022-04-21 07:45:00 【Xiong Xiaohan】
effect
Used to ensure that the function is called later in the program execution . Usually used for cleaning purposes .
Case study
First implement the method :
func createFile(p string) *os.File {
fmt.Println("creating")
f, err := os.Create(p)
if err != nil {
panic(err)
}
return f
}
func writeFile(f *os.File) {
fmt.Println("writing")
fmt.Fprintln(f, "data")
}
func closeFile(f *os.File) {
fmt.Println("closing")
err := f.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error:%v\n", err)
os.Exit(1)
}
}
And then call the method , Realize file creation 、 Data writing 、 File closing function .
f := createFile("/tmp/defer.txt")
defer closeFile(f)
writeFile(f)
The final result is as follows :
creating
writing
closing
版权声明
本文为[Xiong Xiaohan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210627176151.html
边栏推荐
- Common weak passwords in network security devices
- 【WPF】VisualStateManager
- NP and OSPF monitoring and commissioning
- 树状数组
- playwright,selenium,操作ifram元素
- H + background UI framework, click the button to create a new tab
- 论文阅读:Analyzing Third Party Service Dependencies in Modern Web Services
- Use C # to connect with Hangao highgo database to obtain user data table name, table structure, table name and primary key
- Command-Line
- laravel 打印sql
猜你喜欢
随机推荐
虚拟机 主机 ping ssh 校园网 桥接 NET
Enterprise service bus -- Introduction to muleesb
Regular Expressions
Time and Duration and Epoch
URL Parsing
pycharm 最新导入PIL库的方法
标准函数返回值iResult
在vscode 中安装go插件并配置go环境以运行go
mysql where条件后执行规则
数据库日志级别解析:(2022.2.21-2022.2.27)
Command-Line
压缩毕业时间到20岁以前,5岁上小学上5年,提高人口数量
Ctf-rsa decryption script
Nsctf - some topics WP
状态模式(4.4-4.10)
Leetcode 1561.你可以获得的最大硬币数目(Maximum Number of Coins You Can Get)
论文阅读:Cached and Confused: Web Cache Deception in the wild
TCP三次握手和四次挥手简介(2022.4.18-4.24)
【WPF】转换器Converter
栈(C语言)









