知识卡片
映射读取要区分缺席与零值
map把可比较的键映射为值;读取不存在的键会得到值类型零值。因此当零本身有业务意义时,应使用value,ok双结果判断存在性,不能把零直接当作缺席。make和字面量用于创建,delete删除,range遍历次序不作排序保证。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Go Language Foundations · 查看原书依据
Requesting the value of a key that’s not in a map won’t throw an exception (which don’t exist in Go) or return some kind of null value: rather, the zero value for its type is returned. This can be a vary useful feature because it reduces a lot of boilerplate membership testing when working with
参考来源
- 位置:《Cloud Native Go》第03章《Go Language Foundations》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch03.xhtml)
- 结论依据:map把可比较的键映射为值;读取不存在的键会得到值类型零值。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。
- 原始内容:Requesting the value of a key that’s not in a map won’t throw an exception (which don’t exist in Go) or return some kind of null value: rather, the zero value for its type is returned. This can be a vary useful feature because it reduces a lot of boilerplate membership testing when working with