Skip to content

Commit e149a1e

Browse files
committed
atomic examples uses typed variables
1 parent 78539d5 commit e149a1e

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

docs/01-common-patterns/atomic-ops.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ Low-level access to relaxed memory ordering in Go exists internally (e.g., in th
4949
Tracking request counts, dropped packets, or other lightweight stats:
5050

5151
```go
52-
var requests int64
52+
var requests atomic.Int64
5353

5454
func handleRequest() {
55-
atomic.AddInt64(&requests, 1)
55+
requests.Add(1)
5656
}
5757
```
5858

@@ -63,19 +63,19 @@ This code allows multiple goroutines to safely increment a shared counter withou
6363
Simple boolean state shared across threads:
6464

6565
```go
66-
var shutdown int32
66+
var shutdown atomic.Int32
6767

6868
func mainLoop() {
69-
for {
70-
if atomic.LoadInt32(&shutdown) == 1 {
71-
break
72-
}
73-
// do work
74-
}
69+
for {
70+
if shutdown.Load() == 1 {
71+
break
72+
}
73+
// do work
74+
}
7575
}
7676

7777
func stop() {
78-
atomic.StoreInt32(&shutdown, 1)
78+
shutdown.Store(1)
7979
}
8080
```
8181

@@ -86,12 +86,12 @@ This pattern allows one goroutine to signal another to stop. `atomic.LoadInt32`
8686
Replace `sync.Once` when you need more control:
8787

8888
```go
89-
var initialized int32
89+
var initialized atomic.Int32
9090

9191
func maybeInit() {
92-
if atomic.CompareAndSwapInt32(&initialized, 0, 1) {
93-
// initialize resources
94-
}
92+
if initialized.CompareAndSwap(0, 1) {
93+
// initialize resources
94+
}
9595
}
9696
```
9797

docs/01-common-patterns/immutable-data.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ type Config struct {
3232
Maps and slices in Go are reference types. Even if the Config struct isn't changed, someone could accidentally mutate a shared map. To prevent this, we make defensive copies:
3333

3434
```go
35-
func NewConfig(logLevel string, timeout time.Duration, features map[string]bool) Config {
35+
func NewConfig(logLevel string, timeout time.Duration, features map[string]bool) *Config {
3636
copiedFeatures := make(map[string]bool, len(features))
3737
for k, v := range features {
3838
copiedFeatures[k] = v
3939
}
4040

41-
return Config{
41+
return &Config{
4242
LogLevel: logLevel,
4343
Timeout: timeout,
4444
Features: copiedFeatures,
@@ -52,15 +52,15 @@ Now, every config instance is self-contained and safe to share.
5252
Use `atomic.Value` to store and safely update the current config.
5353

5454
```go
55-
var currentConfig atomic.Value
55+
var currentConfig atomic.Pointer[Config]
5656

5757
func LoadInitialConfig() {
5858
cfg := NewConfig("info", 5*time.Second, map[string]bool{"beta": true})
5959
currentConfig.Store(cfg)
6060
}
6161

62-
func GetConfig() Config {
63-
return currentConfig.Load().(Config)
62+
func GetConfig() *Config {
63+
return currentConfig.Load()
6464
}
6565
```
6666

@@ -97,16 +97,16 @@ type RoutingTable struct {
9797
To ensure immutability, we deep-copy the slice of routes when constructing a new routing table.
9898

9999
```go
100-
func NewRoutingTable(routes []Route) RoutingTable {
100+
func NewRoutingTable(routes []Route) *RoutingTable {
101101
copied := make([]Route, len(routes))
102102
copy(copied, routes)
103-
return RoutingTable{Routes: copied}
103+
return &RoutingTable{Routes: copied}
104104
}
105105
```
106106

107107
### Step 3: Store It Atomically
108108
```go
109-
var currentRoutes atomic.Value
109+
var currentRoutes atomic.Pointer[RoutingTable]
110110

111111
func LoadInitialRoutes() {
112112
table := NewRoutingTable([]Route{
@@ -116,8 +116,8 @@ func LoadInitialRoutes() {
116116
currentRoutes.Store(table)
117117
}
118118

119-
func GetRoutingTable() RoutingTable {
120-
return currentRoutes.Load().(RoutingTable)
119+
func GetRoutingTable() *RoutingTable {
120+
return currentRoutes.Load()
121121
}
122122
```
123123

0 commit comments

Comments
 (0)