Defer 声明的设计理念
【Defer 声明的设计理念】
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.(够本释放是Windows编程中的大问题)
For example, let's look at a function that opens two files and copies the contents of one file to the other:

This works, but there is a bug. If the call to os.Create fails, the function will return without closing the source file. This can be easily remedied by putting a call to src.Close before the second return statement, but if the function were more complex the problem might not be so easily noticed and resolved. By introducing defer statements we can ensure that the files are always closed:

Defer statements allow us to think about closing each file right after opening it, guaranteeing that, regardless of the number of return statements in the function, the files will be closed.
【Defer的三大原则】
The behavior of defer statements is straightforward and predictable. There are three simple rules:
1. A deferred function's arguments are evaluated when the defer statement is evaluated.
In this example, the expression "i" is evaluated when the Println call is deferred. The deferred call will print "0" after the function returns.

2. Deferred function calls are executed in Last In First Out order after_the surrounding function returns.
This function prints "3210":

3. Deferred functions may read and assign to the returning function's named return values.
In this example, a deferred function increments the return value i after the surrounding function returns. Thus, this function returns 2:

参考:http://blog.golang.org/defer-panic-and-recover
Defer 声明的设计理念的更多相关文章
- golang 详解defer
什么是defer defer用来声明一个延迟函数,把这个函数放入到一个调用链表上, 当外部的包含方法return之前,返回参数到调用方法之前调用,也可以说是运行到最外层方法体的"}" ...
- Go 延迟函数 defer 详解
Go 延迟函数 defer 详解 Go 语言中延迟函数 defer 充当着 try...catch 的重任,使用起来也非常简便,然而在实际应用中,很多 gopher 并没有真正搞明白 defer.re ...
- Go:函数、defer
一.函数可赋值给一个变量 示例1: package main import "fmt" func add(a, b int) int { return a + b } func m ...
- golang入门-defer
package main import "fmt" func main() { i := 5 tmap := make(map[string]int, 5) tmap[" ...
- 关于Go defer的详细使用
先抛砖引玉defer的延迟调用:defer特性: . 关键字 defer 用于注册延迟调用. . 这些调用直到 return 前才被执.因此,可以用来做资源清理. . 多个defer语句,按先进后出的 ...
- 5.5 Go defer
5.5 Go defer 程序开发中经常要创建资源(数据库初始化连接,文件句柄,锁等),在程序执行完毕都必须得释放资源,Go提供了defer(延时机制)更方便.更及时的释放资源. 1.内置关键字def ...
- 深入 Go 语言 defer 实现原理
转载请声明出处哦~,本篇文章发布于luozhiyun的博客: https://www.luozhiyun.com/archives/523 本文使用的go的源码 1.15.7 介绍 defer 执行规 ...
- 13. Go 语言网络爬虫
Go 语言网络爬虫 本章将完整地展示一个应用程序的设计.编写和简单试用的全过程,从而把前面讲到的所有 Go 知识贯穿起来.在这个过程中,加深对这些知识的记忆和理解,以及再次说明怎样把它们用到实处.由本 ...
- go语言的初体验
分享最近学习 Go 语言的心得和体会,适合有编程基础的人,因为这里只做经验性的总结概述,不做基础教学的入门知识讲解,如果想要学习编程语言的基础知识,请出门左转进入官方文档,查看基础教学文档. Go 概 ...
随机推荐
- halcon之扫描文档祛底色
halcon之扫描文档祛底色增 很多扫描APP都有祛底色的功能:用于改善成像质量,通常扫描后的图像可能会用于存档或 ...
- XML——概述
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- oracle创建触发器
create or replace trigger 触发器名字 触发时间 触发事件 after(before) insert or update or delete on 表名 for each r ...
- 捕捉过滤器(CaptureFilters)和显示过滤器(DisplayFilters)--Wireshark
Wireshark的基本使用——过滤器 前言 网络上关于Wireshark的教程已有不少,博主就简单介绍一下Wireshark分析数据包时最重要的技巧之一的过滤器..一次性嗅探到的数据包有很多,想要高 ...
- Windows下使用curl命令
curl下载地址: https://curl.haxx.se/download.html 选择对应的版本下载后解压 使用方式(一):在解压后curl.exe所在目录打开cmd,即可使用 使用方式(二) ...
- TCP建立连接的三次握手和TCP连接断开的四次挥手
1. TCP建立连接的3次握手 2. TCP断开连接的四次挥手 [注意]中断连接端可以是Client端,也可以是Server端. 图3—Client端主动发起关闭连接请求 1. 假设Client端主动 ...
- 实例化Bean的方法(基于xml配置)-http://blog.csdn.net/shymi1991/article/details/48153293
实例化Bean的方法(基于xml配置) 标签: spring framework 2015-09-01 13:43 918人阅读 评论(0) 收藏 举报 分类: Spring FrameWork(7 ...
- Oracle Client 10g (instantclient) 精简版安装
今天遇到个软件要求安装oracle client端,于是考虑装精简版本的,就从http://www.oracle.com/technology/software/tech/oci/instantcli ...
- 移植SDL2.2问题及解决方法
项目需要ffmpeg+SDL播放视频,所以不得不移植SDL 根据 <移植SDL最新版本>http://blog.csdn.net/flyyang123456789/article/deta ...
- CentOS 6安装php加速软件Zend Guard(转)
(尚未验证) PHP5.3以上的版本不再支持Zend Optimizer,已经被全新的 Zend Guard Loader 取代,下面是安装Zend Guard具体步骤,以下操作均在终端命令行执行 1 ...