-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.go
53 lines (41 loc) · 883 Bytes
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"sync"
)
type Trip struct {
destination string
}
type Driver struct {
trips []Trip
}
//func (d *Driver) SetTrips(trips []Trip) {
// d.trips = trips
//}
func (d *Driver) SetTrips(trips []Trip) {
// Create a copy of the trips slice
d.trips = make([]Trip, len(trips))
copy(d.trips, trips)
}
type Stats struct {
mu sync.Mutex
counters map[string]int
}
// Snapshot returns the current stats.
func (s *Stats) Snapshot() map[string]int {
s.mu.Lock()
defer s.mu.Unlock()
return s.counters
}
func main() {
stats := &Stats{
counters: map[string]int{"a": 1, "b": 2},
}
// Get a snapshot of the current stats
snapshot := stats.Snapshot()
// Modify the snapshot
snapshot["a"] = 42
// The original stats remain unchanged
fmt.Println(stats.counters) // Output: map[a:1 b:2]
fmt.Println(snapshot) // Output: map[a:42 b:2]
}