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

时间: 2024-11-03 21:44:43

Defer 声明的设计理念的相关文章

golang 详解defer

什么是defer defer用来声明一个延迟函数,把这个函数放入到一个栈上, 当外部的包含方法return之前,返回参数到调用方法之前调用,也可以说是运行到最外层方法体的"}"时调用.我们经常用他来做一些资源的释放,比如关闭io操作 func doSomething(fileName string) { file,err := os.Open(fileName) if err != nil { panic(err) } defer file.Close() } defer 可以保证方法

go 函数和流程控制

if/else分支判断 基本结构如下: 1 if condition1 { 2 } 3 4 if condition1 { 5 6 } else { 7 8 } 9 10 if condition1 { 11 12 } else if condition2 { 13 14 } else if condition3 { 15 } else { 16 } 1 // main 2 package main 3 4 import ( 5 "fmt" 6 "strconv"

Go 从入门到精通(三)字符串,时间,流程控制,函数

一.strings和strconv的使用 strings strings.HasPrefix(s string,preffix string) bool:判断字符串s是否以prefix开头 stirngs.HasSuffix(s string,suffix string) bool:判断字符串s是否以suffix结尾 strings.Index(s string,str string) int:判断str在s中首次出现的位置,如果没有出现,则返回-1 strings.LastIndex(s st

GO 新开发者要注意的陷阱和常见错误

转自:http://colobu.com/2015/09/07/gotchas-and-common-mistakes-in-go-golang/ 初级 开大括号不能放在单独的一行 未使用的变量 未使用的Imports 简式的变量声明仅可以在函数内部使用 使用简式声明重复声明变量 偶然的变量隐藏Accidental Variable Shadowing 不使用显式类型,无法使用"nil"来初始化变量 使用"nil" Slices and Maps Map的容量 字符

Rx学习(一)

RXjava学习资料: https://www.gitbook.com/book/yuxingxin/rxjava-essentials-cn/details 如下只是学习笔记而已,后面添加实战案例,现在只是理论总结: Rxjava语言特点: 1,易于并发从而更好的利用服务器的能力: 2,易于有条件的异步执行: 3,一种更好的方式来避免回调地狱: 4,一种响应式方法. RXjava源于观察者模式: 添加了如下三个缺少的功能: 1,生产者在没有更多数据可用时能够发出信号通知:oncompleted

一个有关Golang Deferred Function 执行顺序的问题

先看一下一段关于defer的解释, 引自<Go程序设计语言> Syntactically, a defer statement is an ordinary function or method call prefixed by the keyword defer.The function and argument expressions are evaluated when the statement is executed, but the actual call is deferred

[Angularjs]asp.net mvc+angularjs+web api单页应用

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 写在前面 最近的工作一直在弄一些h5的单页应用,然后嵌入到app的webview中.之前一直在用angularjs+html+ashx的一套东西.实在是玩腻了.然后就尝试通过asp.net mvc的方式构建单页应用.用到的技术angularjs

Angularjs promise-$q服务详解

var ngApp=angular.module('ngApp',[]); /******************************************************************* * $q为内置服务 ****************************************************************/ ngApp.factory('UserInfoService',['$http','$q',function($http,$q){ r

[Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作

写在前面 前篇文章整理了angularjs学习目录,有园子里的朋友问我要这方面的demo,周末也没什么事,就在之前的单页应用的demo上面添加了增删改查的操作.代码比较简单,这里只列举比较重要的代码片段.完整的代码将在文章下面提供链接. demo 数据来源通过webapi的方式提供.获取对产品的查询,分页,增加商品,删除,修改等操作. webapi using Newtonsoft.Json; using System; using System.Collections.Generic; usi