슬라이스와 맵 받기
맵이나 슬라이스를 함수 매개변수로 전달할 때, 그들에 대한 참조를 저장하면 사용자가 그들을 수정할 수 있다는 것을 기억해야 합니다.
권장하지 않는 방식:
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()