知识卡片
闭包保留外层变量
函数可以存入变量、作为参数或返回值。闭包还关联其使用的外层变量,即使创建它的调用已经结束,状态仍能被后续调用访问。每次创建独立闭包可得到不同状态;同一闭包若被多个goroutine并发调用,读写这些状态仍需要同步。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Go Language Foundations · 查看原书依据
A particularly powerful feature of Go is that anonymous functions have access to the state of their parent, and retain that access even after the parent function has executed. This is, in fact, the definition of a closure. Tip A closure is a nested function that has access to the variables of its parent function,
参考来源
- 位置:《Cloud Native Go》第03章《Go Language Foundations》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch03.xhtml)
- 结论依据:函数可以存入变量、作为参数或返回值。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。
- 原始内容:A particularly powerful feature of Go is that anonymous functions have access to the state of their parent, and retain that access even after the parent function has executed. This is, in fact, the definition of a closure. Tip A closure is a nested function that has access to the variables of its parent function,