Skip to content

Commit c81e8ac

Browse files
committed
feat: add atomic pointer support
1 parent c12bc2e commit c81e8ac

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ package main
9393

9494
import (
9595
"fmt"
96-
pointer "github.com/golang-infrastructure/go-reflect-utils"
96+
"github.com/golang-infrastructure/go-pointer"
9797
)
9898

9999
func main() {
@@ -128,6 +128,19 @@ func main() {
128128
// 返回当前时间的指针
129129
nowPointer := pointer.Now()
130130
fmt.Println(nowPointer) // Output: 2023-05-30 11:46:20.3695476 +0800 CST m=+0.003922101
131+
132+
// atomic.Bool指针 - true
133+
atomicTruePointer := pointer.AtomicTruePointer()
134+
fmt.Println(atomicTruePointer) // Output: &{{} 1}
135+
136+
// atomic.Bool指针 - false
137+
atomicFalsePointer := pointer.AtomicFalsePointer()
138+
fmt.Println(atomicFalsePointer) // Output: &{{} 0}
139+
140+
// atomic.Int64指针
141+
int64Pointer := pointer.AtomicInt64Pointer(128)
142+
fmt.Println(int64Pointer) // Output: &{{} {} 128}
143+
131144
}
132145
```
133146

example/main.go

+13
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,17 @@ func main() {
3737
// 返回当前时间的指针
3838
nowPointer := pointer.Now()
3939
fmt.Println(nowPointer) // Output: 2023-05-30 11:46:20.3695476 +0800 CST m=+0.003922101
40+
41+
// atomic.Bool指针 - true
42+
atomicTruePointer := pointer.AtomicTruePointer()
43+
fmt.Println(atomicTruePointer) // Output: &{{} 1}
44+
45+
// atomic.Bool指针 - false
46+
atomicFalsePointer := pointer.AtomicFalsePointer()
47+
fmt.Println(atomicFalsePointer) // Output: &{{} 0}
48+
49+
// atomic.Int64指针
50+
int64Pointer := pointer.AtomicInt64Pointer(128)
51+
fmt.Println(int64Pointer) // Output: &{{} {} 128}
52+
4053
}

pointer.go

+37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pointer
22

33
import (
44
reflectutils "github.com/golang-infrastructure/go-reflect-utils"
5+
"sync/atomic"
56
"time"
67
)
78

@@ -50,3 +51,39 @@ func FromPointerOrDefault[T any](valuePointer *T, defaultValue T) T {
5051
return *valuePointer
5152
}
5253
}
54+
55+
func AtomicTruePointer() *atomic.Bool {
56+
b := &atomic.Bool{}
57+
b.Store(true)
58+
return b
59+
}
60+
61+
func AtomicFalsePointer() *atomic.Bool {
62+
b := &atomic.Bool{}
63+
b.Store(false)
64+
return b
65+
}
66+
67+
func AtomicUInt64Pointer(value uint64) *atomic.Uint64 {
68+
i := &atomic.Uint64{}
69+
i.Store(value)
70+
return i
71+
}
72+
73+
func AtomicUInt32Pointer(value uint32) *atomic.Uint32 {
74+
i := &atomic.Uint32{}
75+
i.Store(value)
76+
return i
77+
}
78+
79+
func AtomicInt64Pointer(value int64) *atomic.Int64 {
80+
i := &atomic.Int64{}
81+
i.Store(value)
82+
return i
83+
}
84+
85+
func AtomicInt32Pointer(value int32) *atomic.Int32 {
86+
i := &atomic.Int32{}
87+
i.Store(value)
88+
return i
89+
}

0 commit comments

Comments
 (0)