Review generated price store

from Maps
Go 1.27 advanced 6 min 4 issues to find

Review this generated price store against the stated task.

Build a zero-value-ready price store that distinguishes a missing item from a free item, returns a snapshot callers cannot mutate, and lists only stored keys in sorted order.

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
}

generated code is illustrative, not from any one model

Open in playground
Report an error