golang 函数三 (延迟调用)

100018 · · 5470 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Go语言提供defer关键字,用于延迟调用,延迟到当函数返回前被执行,多用于资源释放、解锁以及错误处理等操作。比如:

func聽main()聽{
聽聽聽聽f,聽err聽:=聽createFile("defer.txt")
聽聽聽聽if聽err聽!=聽nil聽{
聽聽聽聽聽聽聽聽fmt.Println(err.Error())
聽聽聽聽聽聽聽聽return
聽聽聽聽}聽聽聽
聽聽聽聽defer聽closeFile(f)
聽聽聽聽writeFile(f)
}

func聽createFile(filePath聽string)聽(*os.File,聽error)聽{
聽聽聽聽f,聽err聽:=聽os.Create(filePath)
聽聽聽聽if聽err聽!=聽nil聽{
聽聽聽聽聽聽聽聽return聽nil,聽err聽
聽聽聽聽}聽聽聽
聽聽聽聽return聽f,聽nil聽
}

func聽writeFile(f聽*os.File)聽{
聽聽聽聽fmt.Println("write聽file")
聽聽聽聽fmt.Fprintln(f,聽"hello聽gopher!")
}

func聽closeFile(f聽*os.File)聽{
聽聽聽聽fmt.Println("close聽file")
聽聽聽聽f.Close()
}

如果一个函数内引用了多个defer,它们的执行顺序是怎么样的呢?比如:

package聽main

func聽main()聽{
	defer聽println("a")
	defer聽println("b")
}
输出:
b
a


如果函数中引入了panic函数,那么延迟调用defer会不会被执行呢?比如:

func聽main()聽{
聽聽聽聽defer聽println("a")
聽聽聽聽panic("d")
聽聽聽聽defer聽println("b")
}
输出:
a
panic:聽d

goroutine聽1聽[running]:
panic(0x48a560,聽0xc42000a340)
	/root/data/go/src/runtime/panic.go:500聽+0x1a1
main.main()
	/root/data/workspace/src/defer/main.go:7聽+0x107
exit聽status聽2

日常开发中,一定要记住defer是在函数结束时才被调用的,如果应用不合理,可能会造成资源浪费,给gc带来压力,甚至造成逻辑错误,比如:

func聽main()聽{
聽聽聽聽for聽i聽:=聽0;i聽<聽10000;i++{
聽聽聽聽聽聽聽聽filePath聽:=聽fmt.Sprintf("/data/log/%d.log",聽i)
聽聽聽聽聽聽聽聽fp,聽err聽:=聽os.Open(filePath)
聽聽聽聽聽聽聽聽if聽err聽!=聽nil{
聽聽聽聽聽聽聽聽聽聽聽聽continue
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽defef聽fp.Close()聽聽聽聽//这是要在main函数返回时才会执行的,不是在循环结束后执行,延迟调用,导致占用资源
聽聽聽聽聽聽聽聽//do聽stuff...
聽聽聽聽}
}

修改方案是直接调用Close函数或将逻辑封装成独立函数,比如:

func聽logAnalisys(p聽string){
聽聽聽聽fp,聽err聽:=聽os.Open(p)
聽聽聽聽if聽err聽!=聽nil{
聽聽聽聽聽聽聽聽continue
聽聽聽聽}
聽聽聽聽defef聽fp.Close()
聽聽聽聽//do聽stuff
}

func聽main()聽{
聽聽聽聽for聽i聽:=聽0;i聽<聽10000;i++{
聽聽聽聽聽聽聽聽filePath聽:=聽fmt.Sprintf("/data/log/%d.log",聽i)
聽聽聽聽聽聽聽聽logAnalisys(filePath)聽聽聽聽//将业务逻辑独立封装成函数
聽聽聽聽}
}


在性能方面,延迟调用花费的代价也很大,因为这个过程包括注册、调用等操作,还有额外的内存开销。比如:

package聽main

import聽"testing"
import聽"fmt"
import聽"sync"

var聽m聽sync.Mutex

func聽test(){
	m.Lock()
	m.Unlock()
}

func聽testCap(){
	m.Lock()
	defer聽m.Unlock()
}

func聽BenchmarkTest(t聽*testing.B){
	for聽i:=聽0;i聽<聽t.N;聽i++{
		test()
	}
}

func聽BenchmarkTestCap(t聽*testing.B){
	for聽i:=聽0;i聽<聽t.N;聽i++{
		testCap()
	}
}

func聽main(){
	resTest聽:=聽testing.Benchmark(BenchmarkTest)
	fmt.Printf("BenchmarkTest聽\t聽%d,聽%d聽ns/op,%d聽allocs/op,聽%d聽B/op\n",聽resTest.N,聽resTest.NsPerOp(),聽resTest.AllocsPerOp(),聽resTest.AllocedBytesPerOp())
	resTest聽=聽testing.Benchmark(BenchmarkTestCap)
	fmt.Printf("BenchmarkTestCap聽\t聽%d,聽%d聽ns/op,%d聽allocs/op,聽%d聽B/op\n",聽resTest.N,聽resTest.NsPerOp(),聽resTest.AllocsPerOp(),聽resTest.AllocedBytesPerOp())
}
输出:
BenchmarkTest聽	聽50000000,聽27聽ns/op,0聽allocs/op,聽0聽B/op
estCap聽	聽20000000,聽112聽ns/op,0聽allocs/op,聽0聽B/op

在要求高性能的高并发场景下,应避免使用延迟调用。

本文出自 “博学于文,约之于礼” 博客,转载请与作者联系!


有疑问加站长微信联系(非本文作者)

本文来自:51CTO博客

感谢作者:100018

查看原文:golang 函数三 (延迟调用)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

5470 次点击  
加入收藏 微博
1 回复  |  直到 2018-11-09 14:37:13
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传