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. BootStrap--scroll

    滚动侦测 滚动侦测基本使用方法为: <body data-spy="scroll"> <nav class="navbar navbar-default ...

  2. 互评Alpha版本

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2323] 队名:二次元梦之队 组长:刘莹莹 组员:周昊 潘世维  王玉潘 赵美增 ...

  3. MDI窗体及涉及到的相关问题

    MDI窗体是个什么东东尼?多文档窗体唠,相对的还有个SDI,就是单文档窗体.一般情况下我们会将软件的主窗体设置为MDI窗体.那么在主窗体的菜单部分调出来的其他窗体就是主窗体的子窗体了. (1)在主窗体 ...

  4. Centos7环境下安装python3.6.4 并与python2共存

    最近安装了新系统centos7,本身自带python2.7,但是由于需要使用python3 来运行一些应用,所以想到在centos环境下安装python3,并保证其和python2 可以共存. 步骤如 ...

  5. vue music 歌单组件

    在data里面定义 discList: [] methods: { _getRecommend() { getRecommend().then((res) => { if(res.code == ...

  6. leisure time

    终于把论文翻译完了,天哪,现在感觉解脱一般. 这些天看电视,玩游戏,也不止学了写什么,现在调整了下心情,重新确定下目标吧. 最近很想学Python和Qt,哎,技术永远都是学不完了,理解操作系统和组成原 ...

  7. 《DSP using MATLAB》Problem 2.10

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  8. 设计模式(Python)-简单工厂,工厂方法和抽象工厂模式

    本系列文章是希望将软件项目中最常见的设计模式用通俗易懂的语言来讲解清楚,并通过Python来实现,每个设计模式都是围绕如下三个问题: 为什么?即为什么要使用这个设计模式,在使用这个模式之前存在什么样的 ...

  9. 安装 Android Studio 2.3 详细过程及错误解决

    因为要开发Android5.0的缘故,抛弃了eclipse转到了Android Studio,第一次使用就是遇到了许多问题,终于是解决问题了,特意写一篇博文给各位要准备从eclipse转到Androi ...

  10. mysql having,group by查询去除重复记录

    http://m.jb51.net/article/39302.htm 可以这样去理解group by和聚合函数 http://www.cnblogs.com/wuguanglei/p/4229938 ...