知识卡片
零分不能天然代表不存在
GetPlayerScore只返回int,而map查不到的零值与真实0分相同。本例用score==0返回404,相当于预设“存在玩家必有胜场”;若业务允许0分玩家,接口需另外表达是否存在,例如(score,exists)或显式结果。返回值应描述查询结果,不能只迎合处理器的分支需要。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
- · HTTP 服务器
原书出处 · 1 处
- HTTP 服务器 · 查看原书依据
p.store.GetPlayerScore(player) if score == 0 { w.WriteHeader(http.StatusNotFound) } fmt.Fprint(w, score) }
参考来源
- 位置:《通过测试学习 Go 编程》HTTP 服务器,spine023(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/22.html)。
- 结论依据:原文以0判缺失且存储直接返回map值,无法区分两个业务状态。
- 原始内容: p.store.GetPlayerScore(player) if score == 0 { w.WriteHeader(http.StatusNotFound) } fmt.Fprint(w, score) }