スライスとマップの受け取り
マップやスライスが関数パラメータとして渡される場合、それらへの参照を保存すると、ユーザーがそれらを変更できることを覚えておいてください。
非推奨の方法:
func (d *Driver) SetTrips(trips []Trip) {
d.trips = trips
}
trips := ...
d1.SetTrips(trips)
// d1.tripsを変更しようとしていますか?
trips[0] = ...
推奨される方法:
func (d *Driver) SetTrips(trips []Trip) {
d.trips = make([]Trip, len(trips))
copy(d.trips, trips)
}
trips := ...
d1.SetTrips(trips)
// trips[0]を変更していますが、d1.tripsには影響しません
trips[0] = ...
スライスやマップの返却
同様に、内部状態を公開するマップやスライスをユーザーが変更することに注意してください。
非推奨の方法:
type Stats struct {
mu sync.Mutex
counters map[string]int
}
// Snapshotは現在の状態を返します
func (s *Stats) Snapshot() map[string]int {
s.mu.Lock()
defer s.mu.Unlock()
return s.counters
}
// snapshotはもはやミューテックスで保護されていないため、
// snapshotへのアクセスはデータ競合の対象となり、stats.countersに影響を与えます
snapshot := stats.Snapshot()
推奨される方法:
type Stats struct {
mu sync.Mutex
counters map[string]int
}
func (s *Stats) Snapshot() map[string]int {
s.mu.Lock()
defer s.mu.Unlock()
result := make(map[string]int, len(s.counters))
for k, v := range s.counters {
result[k] = v
}
return result
}
// snapshotは今やコピーです
snapshot := stats.Snapshot()