<5> go 上下文传递context
来源:互联网 发布:折800淘宝女装 编辑:程序博客网 时间:2024/05/23 20:51
有go有channal,slick,map,interface,func等等,其中func可以当作参数传递的这个功能也是很多动态语言支持的。
现在看下怎么使用
import ( "fmt")type Do struct { Name string}func (this Do) Lock() { fmt.Println(this.Name + " Lock ...")}func (this Do) UnLock() { fmt.Println(this.Name + " Unlock ~")}// 假设需要做加锁解锁func Say() { do := Do{Name: "Say"} do.Lock() defer do.UnLock() fmt.Println("say doing")}// 假设需要做加锁解锁func Fuck() { do := Do{Name: "Fuck"} do.Lock() defer do.UnLock() fmt.Println("fuck doing")}// 改造 -withContextfunc withDoContext(fn func()) { do := Do{Name: "do"} do.Lock() defer do.UnLock() fn()}func Quit() { withDoContext(func() { fmt.Println("quit doing") })}func Reboot() { fmt.Println("reboot doing")}// 带有返回值的context// 改造 -withContextfunc withDoContextString(fn func() string) string { do := Do{Name: "do"} do.Lock() defer do.UnLock() return fn()}func main() { fmt.Println("Start Main func()") Say() Fuck() // 改造后 Quit() withDoContext(Reboot) msg := withDoContextString(func() string { return "hahah" }) fmt.Println("msg ", msg)}
输出:
Start Main func()Say Lock ...say doingSay Unlock ~Fuck Lock ...fuck doingFuck Unlock ~do Lock ...quit doingdo Unlock ~do Lock ...reboot doingdo Unlock ~do Lock ...do Unlock ~msg hahah
0 0
- <5> go 上下文传递context
- Context(上下文)
- Context上下文
- Context 上下文
- Context---上下文
- Context上下文
- context上下文
- Context上下文
- 上下文 Context
- 上下文context, 上下文菜单解释
- 关于上下文(Context)
- [android]Context 上下文
- 获取上下文环境context
- java context上下文
- 释意:Context(上下文)
- 释意:Context(上下文)
- shape context (形状上下文)
- 上下文Context的区别
- 用Swing写个scrapy爬虫图形界面
- Introducing IPython Notebook
- javascript两种禁止一个函数没有运行结束时就再次调用的方法
- 偶有所感
- 昌大软院大神养成计划之网页设计之路第二天
- <5> go 上下文传递context
- A==B?
- HTTP协议学习(三)——连接管理
- 3.Spring MVC 4.1-@RequestMapping
- 第十周练习 1006 月之数
- Python实现爬虫统计学校BBS男女比例(一)前期准备、方案分析
- 算法练习六:旋转数组的最小数字
- java界面windowbuilder
- HDU 免费馅饼