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.
This commit is contained in:
L1nSn0w
2025-09-02 15:44:42 +08:00
committed by GitHub
parent f15fcec338
commit a08a8f4162

View File

@@ -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本身导致的而是因为它们都会等待循环结束后再执行函数值。