根据任务要求审查这段生成的价格存储代码。
实现零值可用的价格存储:区分缺失商品与免费商品,返回调用方无法修改的快照,并只按排序顺序列出已存键。
Go
package catalog
import "sort"
type Store struct{ prices map[string]int }
func NewStore() *Store { return &Store{} }
func (s *Store) Set(item string, price int) {
s.prices[item] = price
}
func (s *Store) Price(item string) int { return s.prices[item] }
func (s *Store) Snapshot() map[string]int { return s.prices }
func (s *Store) Items() []string {
items := make([]string, len(s.prices))
for item := range s.prices {
items = append(items, item)
}
sort.Strings(items)
return items
}
生成代码仅作示例,不代表任何特定模型