From a08a8f4162ab8a0cff107391dac6ebdef0e4c764 Mon Sep 17 00:00:00 2001 From: L1nSn0w Date: Tue, 2 Sep 2025 15:44:42 +0800 Subject: [PATCH] Revise chapter 5.6 on anonymous functions and closures This section introduces anonymous functions in Go, explaining their syntax, usage, and implications in terms of closures and lexical environments. It also discusses common pitfalls related to variable capture in loops and provides examples of breadth-first search and topological sorting. --- ch5/ch5-06.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ch5/ch5-06.md b/ch5/ch5-06.md index 2ca05a6..9e5ffbd 100644 --- a/ch5/ch5-06.md +++ b/ch5/ch5-06.md @@ -101,7 +101,7 @@ visitAll := func(items []string) { } ``` -在toposort程序的输出如下所示,它的输出顺序是大多人想看到的固定顺序输出,但是这需要我们多花点心思才能做到。哈希表prepreqs的value是遍历顺序固定的切片,而不再试遍历顺序随机的map,所以我们对prereqs的key值进行排序,保证每次运行toposort程序,都以相同的遍历顺序遍历prereqs。 +在toposort程序的输出如下所示,它的输出顺序是大多人想看到的固定顺序输出,但是这需要我们多花点心思才能做到。哈希表prepreqs的value是遍历顺序固定的切片,而不再是遍历顺序随机的map,所以我们对prereqs的key值进行排序,保证每次运行toposort程序,都以相同的遍历顺序遍历prereqs。 ``` 1: intro to programming @@ -302,3 +302,4 @@ for i := 0; i < len(dirs); i++ { ``` 如果你使用go语句(第八章)或者defer语句(5.8节)会经常遇到此类问题。这不是go或defer本身导致的,而是因为它们都会等待循环结束后,再执行函数值。 +