知识卡片
构造入口建立可写映射
给map类型字段分配零值struct,不会创建可写的map;首次赋值会失败。NewInMemoryPlayerStore用map字面量建立写入前提,并让main和测试使用相同入口。构造函数是约定,不会从语言层面禁止调用方绕过它。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
- · HTTP 服务器
原书出处 · 1 处
- HTTP 服务器 · 查看原书依据
func NewInMemoryPlayerStore() *InMemoryPlayerStore { return &InMemoryPlayerStore{map[string]int{}} } type InMemoryPlayerStore struct{ store map[string
参考来源
- 位置:《通过测试学习 Go 编程》HTTP 服务器,spine023(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/22.html)。
- 结论依据:原文为RecordWin递增增加已初始化map的构造函数。
- 原始内容:func NewInMemoryPlayerStore() *InMemoryPlayerStore { return &InMemoryPlayerStore{map[string]int{}} } type InMemoryPlayerStore struct{ store map[string