知识卡片
控制结构共享局部作用域
Go以for表达计数、条件和持续循环,range为集合提供遍历。if和switch可带局部初始化语句,其变量只在对应语句范围内可见;switch默认不穿透下一个分支,需显式fallthrough。选择形式时应保留清晰的边界和终止条件,而非依赖其他语言的默认语义。
典型形式(教学示例):for i := 0; i < n; i++ { /* 工作 */ };if v, ok := m[key]; ok { /* 使用v */ }。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Go Language Foundations · 查看原书依据
The two constructs are not equivalent however: in the former example err is scoped only to the if statement; in the latter example err is visible to the entire containing function. The switch Statement As in other languages, Go provides a switch statement that provides a way to more concisely express a series of if-then-else
参考来源
- 位置:《Cloud Native Go》第03章《Go Language Foundations》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch03.xhtml)
- 结论依据:Go以for表达计数、条件和持续循环,range为集合提供遍历。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。
- 原始内容:The two constructs are not equivalent however: in the former example err is scoped only to the if statement; in the latter example err is visible to the entire containing function. The switch Statement As in other languages, Go provides a switch statement that provides a way to more concisely express a series of if-then-else