File tree Expand file tree Collapse file tree 4 files changed +12
-14
lines changed
Expand file tree Collapse file tree 4 files changed +12
-14
lines changed Original file line number Diff line number Diff line change @@ -168,7 +168,7 @@ <h2 id="56-匿名函数"><a class="header" href="#56-匿名函数">5.6. 匿名
168168< p > 更为重要的是,通过这种方式定义的函数可以访问完整的词法环境(lexical environment),这意味着在函数中定义的内部函数可以引用该函数的变量,如下例所示:</ p >
169169< p > < u > < i > gopl.io/ch5/squares</ i > </ u > </ p >
170170< pre > < code class ="language-Go "> // squares返回一个匿名函数。
171- // 该匿名函数每次被调用时都会返回下一个数的平方的函数
171+ // 该匿名函数每次被调用时都会返回下一个数的平方。
172172func squares() func() int {
173173 var x int
174174 return func() int {
@@ -178,11 +178,10 @@ <h2 id="56-匿名函数"><a class="header" href="#56-匿名函数">5.6. 匿名
178178}
179179func main() {
180180 f := squares()
181- // () 函数调用符号
182- fmt.Println(f()()) // "1"
183- fmt.Println(f()()) // "4"
184- fmt.Println(f()()) // "9"
185- fmt.Println(f()()) // "16"
181+ fmt.Println(f()) // "1"
182+ fmt.Println(f()) // "4"
183+ fmt.Println(f()) // "9"
184+ fmt.Println(f()) // "16"
186185}
187186</ code > </ pre >
188187< p > 函数squares返回另一个类型为 func() int 的函数。对squares的一次调用会生成一个局部变量x并返回一个匿名函数。每次调用匿名函数时,该函数都会先使x的值加1,再返回x的平方。第二次调用squares时,会生成第二个x变量,并返回一个新的匿名函数。新匿名函数操作的是第二个x变量。</ p >
Original file line number Diff line number Diff line change @@ -3972,7 +3972,7 @@ <h3 id="542-文件结尾错误eof"><a class="header" href="#542-文件结尾错
39723972<p>更为重要的是,通过这种方式定义的函数可以访问完整的词法环境(lexical environment),这意味着在函数中定义的内部函数可以引用该函数的变量,如下例所示:</p>
39733973<p><u><i>gopl.io/ch5/squares</i></u></p>
39743974<pre><code class="language-Go">// squares返回一个匿名函数。
3975- // 该匿名函数每次被调用时都会返回下一个数的平方的函数
3975+ // 该匿名函数每次被调用时都会返回下一个数的平方。
39763976func squares() func() int {
39773977 var x int
39783978 return func() int {
@@ -3982,11 +3982,10 @@ <h3 id="542-文件结尾错误eof"><a class="header" href="#542-文件结尾错
39823982}
39833983func main() {
39843984 f := squares()
3985- // () 函数调用符号
3986- fmt.Println(f()()) // "1"
3987- fmt.Println(f()()) // "4"
3988- fmt.Println(f()()) // "9"
3989- fmt.Println(f()()) // "16"
3985+ fmt.Println(f()) // "1"
3986+ fmt.Println(f()) // "4"
3987+ fmt.Println(f()) // "9"
3988+ fmt.Println(f()) // "16"
39903989}
39913990</code></pre>
39923991<p>函数squares返回另一个类型为 func() int 的函数。对squares的一次调用会生成一个局部变量x并返回一个匿名函数。每次调用匿名函数时,该函数都会先使x的值加1,再返回x的平方。第二次调用squares时,会生成第二个x变量,并返回一个新的匿名函数。新匿名函数操作的是第二个x变量。</p>
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments