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 声明的设计理念的更多相关文章

  1. golang 详解defer

    什么是defer defer用来声明一个延迟函数,把这个函数放入到一个调用链表上, 当外部的包含方法return之前,返回参数到调用方法之前调用,也可以说是运行到最外层方法体的"}" ...

  2. Go 延迟函数 defer 详解

    Go 延迟函数 defer 详解 Go 语言中延迟函数 defer 充当着 try...catch 的重任,使用起来也非常简便,然而在实际应用中,很多 gopher 并没有真正搞明白 defer.re ...

  3. Go:函数、defer

    一.函数可赋值给一个变量 示例1: package main import "fmt" func add(a, b int) int { return a + b } func m ...

  4. golang入门-defer

    package main import "fmt" func main() { i := 5 tmap := make(map[string]int, 5) tmap[" ...

  5. 关于Go defer的详细使用

    先抛砖引玉defer的延迟调用:defer特性: . 关键字 defer 用于注册延迟调用. . 这些调用直到 return 前才被执.因此,可以用来做资源清理. . 多个defer语句,按先进后出的 ...

  6. 5.5 Go defer

    5.5 Go defer 程序开发中经常要创建资源(数据库初始化连接,文件句柄,锁等),在程序执行完毕都必须得释放资源,Go提供了defer(延时机制)更方便.更及时的释放资源. 1.内置关键字def ...

  7. 深入 Go 语言 defer 实现原理

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客: https://www.luozhiyun.com/archives/523 本文使用的go的源码 1.15.7 介绍 defer 执行规 ...

  8. 13. Go 语言网络爬虫

    Go 语言网络爬虫 本章将完整地展示一个应用程序的设计.编写和简单试用的全过程,从而把前面讲到的所有 Go 知识贯穿起来.在这个过程中,加深对这些知识的记忆和理解,以及再次说明怎样把它们用到实处.由本 ...

  9. go语言的初体验

    分享最近学习 Go 语言的心得和体会,适合有编程基础的人,因为这里只做经验性的总结概述,不做基础教学的入门知识讲解,如果想要学习编程语言的基础知识,请出门左转进入官方文档,查看基础教学文档. Go 概 ...

随机推荐

  1. VS2017打包安装程序

    VS2017 并不自带安装部署项目,需要在[扩展和更新]中安装插件:Microsoft Visual Studio 2017 Installer Projects(现更名为Microsoft Visu ...

  2. maven中jar、war、pom的区别

    一.maven的三种packaging方式 pom是maven依赖文件 jar是java普通项目打包 war是java web项目打包 pom:打出来可以作为其他项目的maven依赖,在工程A中添加工 ...

  3. 2019年微信小程序1月TOP100榜单

  4. Entity Framework 入门

    Entity Framework的全称是ADO.NET Entity Framework,是微软开发的基于ADO.NET的ORM(Object/Relational Mapping)框架. Entit ...

  5. 64位的ubuntu14.04 LTS安装 Linux交叉编译工具链及32位“ia32-libs”依赖库

    ubuntu又迎来了其新一代的长期支持版本 14.04 LTS,其带来了许多令人期待的新特新,遂决定进行升级. 装好了64位版本及安装 Linux交叉编译工具链 运行GCC,${CROSS_COMPI ...

  6. maven 笔记:maven Could not create the Java Virtual Machine

    1.安装好maven,在cmd中运行mvn –v,报错:“maven  Could not create the Java Virtual Machine” 2.分析:这是跟jvm有关,在cmd中运行 ...

  7. 多目标跟踪baseline methods

    参考文献: MOTChallenge 2015: Towards a Benchmark for Multi-Target TrackingLaura Leal-Taix ´e, Anton Mila ...

  8. POJ 2236:Wireless Network(并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 36363   Accepted: 150 ...

  9. Windows 下的高 DPI 应用开发(UWP / WPF / Windows Forms / Win32)

    本文将介绍 Windows 系统中高 DPI 开发的基础知识.由于涉及到坐标转换,这种转换经常发生在计算的不知不觉中:所以无论你使用哪种 Windows 下的 UI 框架进行开发,你都需要了解这些内容 ...

  10. BinaryFormatter、SoapFormatter、XML3种序列化

    序列化和反序列化我们可能经常会听到,其实通俗一点的解释,序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用.我想最主要的作用有:1.在进程下次 ...