知识卡片
缺失与存着零值不是一回事
单结果map查询在键不存在时给元素零值,字符串场景得到空串;有该键且定义恰为空串也会同样显示。双结果的ok才区分存在性,Dictionary据此将缺失转为ErrNotFound。错误是字典API的选择,map本身不会因普通缺失查找就panic。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
- · Maps
原书出处 · 1 处
- Maps · 查看原书依据
rch(word string) (string, error) { definition, ok := d[word] if !ok { return "", errors.New("could not find the word you were looking for") } return d
参考来源
- 位置:《通过测试学习 Go 编程》Maps,spine011(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/10.html)。
- 结论依据:Search由直接d[word]改为双结果与错误。
- 原始内容:rch(word string) (string, error) { definition, ok := d[word] if !ok { return "", errors.New("could not find the word you were looking for") } return d