|
1 | | -# pointer helpers |
| 1 | +# pointerhelpers |
2 | 2 |
|
3 | | -Go repo to hold all the helpers for pointers to save rewriting the same code every time |
| 3 | +`pointerhelpers` is a Go package designed to reduce boilerplate code when working with pointers. This package provides helper functions for various data types, including `bool`, `int` (and its variations), `float` (and its variations), `complex` (and its variations), and `string`. By using this package, you can streamline your code and enhance readability. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install the `pointerhelpers` package, use the following command: |
| 8 | + |
| 9 | +```bash |
| 10 | +go get github.com/xorima/pointerhelpers |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +The `pointerhelpers` package provides functions to obtain pointers to values and to safely retrieve values from pointers. Below are the examples for each supported type: |
| 16 | + |
| 17 | +### Boolean |
| 18 | + |
| 19 | +```go |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "github.com/xorima/pointerhelpers" |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + b := true |
| 29 | + bPtr := pointerhelpers.Bool(b) |
| 30 | + fmt.Println(*bPtr) // Output: true |
| 31 | + |
| 32 | + nilBoolPtr := (*bool)(nil) |
| 33 | + fmt.Println(pointerhelpers.BoolValue(nilBoolPtr)) // Output: false |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Integers |
| 38 | + |
| 39 | +#### int |
| 40 | + |
| 41 | +```go |
| 42 | +package main |
| 43 | + |
| 44 | +import ( |
| 45 | + "fmt" |
| 46 | + "github.com/xorima/pointerhelpers" |
| 47 | +) |
| 48 | + |
| 49 | +func main() { |
| 50 | + i := 42 |
| 51 | + iPtr := pointerhelpers.Int(i) |
| 52 | + fmt.Println(*iPtr) // Output: 42 |
| 53 | + |
| 54 | + nilIntPtr := (*int)(nil) |
| 55 | + fmt.Println(pointerhelpers.IntValue(nilIntPtr)) // Output: 0 |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +#### Other integer types |
| 60 | + |
| 61 | +The package supports other integer types similarly: `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`. |
| 62 | + |
| 63 | +### Floating Point Numbers |
| 64 | + |
| 65 | +#### float32 |
| 66 | + |
| 67 | +```go |
| 68 | +package main |
| 69 | + |
| 70 | +import ( |
| 71 | + "fmt" |
| 72 | + "github.com/xorima/pointerhelpers" |
| 73 | +) |
| 74 | + |
| 75 | +func main() { |
| 76 | + f := float32(3.14) |
| 77 | + fPtr := pointerhelpers.Float32(f) |
| 78 | + fmt.Println(*fPtr) // Output: 3.14 |
| 79 | + |
| 80 | + nilFloat32Ptr := (*float32)(nil) |
| 81 | + fmt.Println(pointerhelpers.Float32Value(nilFloat32Ptr)) // Output: 0 |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +#### float64 |
| 86 | + |
| 87 | +```go |
| 88 | +package main |
| 89 | + |
| 90 | +import ( |
| 91 | + "fmt" |
| 92 | + "github.com/xorima/pointerhelpers" |
| 93 | +) |
| 94 | + |
| 95 | +func main() { |
| 96 | + f := 3.14 |
| 97 | + fPtr := pointerhelpers.Float64(f) |
| 98 | + fmt.Println(*fPtr) // Output: 3.14 |
| 99 | + |
| 100 | + nilFloat64Ptr := (*float64)(nil) |
| 101 | + fmt.Println(pointerhelpers.Float64Value(nilFloat64Ptr)) // Output: 0 |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Complex Numbers |
| 106 | + |
| 107 | +#### complex64 |
| 108 | + |
| 109 | +```go |
| 110 | +package main |
| 111 | + |
| 112 | +import ( |
| 113 | + "fmt" |
| 114 | + "github.com/xorima/pointerhelpers" |
| 115 | +) |
| 116 | + |
| 117 | +func main() { |
| 118 | + c := complex64(1 + 2i) |
| 119 | + cPtr := pointerhelpers.Complex64(c) |
| 120 | + fmt.Println(*cPtr) // Output: (1+2i) |
| 121 | + |
| 122 | + nilComplex64Ptr := (*complex64)(nil) |
| 123 | + fmt.Println(pointerhelpers.Complex64Value(nilComplex64Ptr)) // Output: (0+0i) |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +#### complex128 |
| 128 | + |
| 129 | +```go |
| 130 | +package main |
| 131 | + |
| 132 | +import ( |
| 133 | + "fmt" |
| 134 | + "github.com/xorima/pointerhelpers" |
| 135 | +) |
| 136 | + |
| 137 | +func main() { |
| 138 | + c := complex(1 + 2i) |
| 139 | + cPtr := pointerhelpers.Complex128(c) |
| 140 | + fmt.Println(*cPtr) // Output: (1+2i) |
| 141 | + |
| 142 | + nilComplex128Ptr := (*complex128)(nil) |
| 143 | + fmt.Println(pointerhelpers.Complex128Value(nilComplex128Ptr)) // Output: (0+0i) |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +#### Embedding Complex64Helper |
| 148 | + |
| 149 | +For users who want to embed `Complex64Helper` into their custom types to gain the pointer helper functionalities directly, the package provides the `Complex64Helper` struct. |
| 150 | + |
| 151 | +```go |
| 152 | +package main |
| 153 | + |
| 154 | +import ( |
| 155 | + "fmt" |
| 156 | + "github.com/xorima/pointerhelpers" |
| 157 | +) |
| 158 | + |
| 159 | +type MyStruct struct { |
| 160 | + pointerhelpers.Complex64Helper |
| 161 | +} |
| 162 | + |
| 163 | +func main() { |
| 164 | + s := MyStruct{} |
| 165 | + c := complex64(1 + 2i) |
| 166 | + cPtr := s.Complex64(c) |
| 167 | + fmt.Println(*cPtr) // Output: (1+2i) |
| 168 | + |
| 169 | + nilComplex64Ptr := (*complex64)(nil) |
| 170 | + fmt.Println(s.Complex64Value(nilComplex64Ptr)) // Output: (0+0i) |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### String |
| 175 | + |
| 176 | +```go |
| 177 | +package main |
| 178 | + |
| 179 | +import ( |
| 180 | + "fmt" |
| 181 | + "github.com/xorima/pointerhelpers" |
| 182 | +) |
| 183 | + |
| 184 | +func main() { |
| 185 | + s := "hello" |
| 186 | + sPtr := pointerhelpers.String(s) |
| 187 | + fmt.Println(*sPtr) // Output: hello |
| 188 | + |
| 189 | + nilStringPtr := (*string)(nil) |
| 190 | + fmt.Println(pointerhelpers.StringValue(nilStringPtr)) // Output: "" |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## Full API Reference |
| 195 | + |
| 196 | +### Functions |
| 197 | + |
| 198 | +- `Bool(v bool) *bool` |
| 199 | +- `BoolValue(v *bool) bool` |
| 200 | +- `Int(v int) *int` |
| 201 | +- `IntValue(v *int) int` |
| 202 | +- `Int8(v int8) *int8` |
| 203 | +- `Int8Value(v *int8) int8` |
| 204 | +- `Int16(v int16) *int16` |
| 205 | +- `Int16Value(v *int16) int16` |
| 206 | +- `Int32(v int32) *int32` |
| 207 | +- `Int32Value(v *int32) int32` |
| 208 | +- `Int64(v int64) *int64` |
| 209 | +- `Int64Value(v *int64) int64` |
| 210 | +- `Uint(v uint) *uint` |
| 211 | +- `UintValue(v *uint) uint` |
| 212 | +- `Uint8(v uint8) *uint8` |
| 213 | +- `Uint8Value(v *uint8) uint8` |
| 214 | +- `Uint16(v uint16) *uint16` |
| 215 | +- `Uint16Value(v *uint16) uint16` |
| 216 | +- `Uint32(v uint32) *uint32` |
| 217 | +- `Uint32Value(v *uint32) uint32` |
| 218 | +- `Uint64(v uint64) *uint64` |
| 219 | +- `Uint64Value(v *uint64) uint64` |
| 220 | +- `Float32(v float32) *float32` |
| 221 | +- `Float32Value(v *float32) float32` |
| 222 | +- `Float64(v float64) *float64` |
| 223 | +- `Float64Value(v *float64) float64` |
| 224 | +- `Complex64(v complex64) *complex64` |
| 225 | +- `Complex64Value(v *complex64) complex64` |
| 226 | +- `Complex128(v complex128) *complex128` |
| 227 | +- `Complex128Value(v *complex128) complex128` |
| 228 | +- `String(v string) *string` |
| 229 | +- `StringValue(v *string) string` |
| 230 | + |
| 231 | +### Structs |
| 232 | + |
| 233 | +- `Complex64Helper` |
| 234 | + |
| 235 | +The `Complex64Helper` struct can be embedded into your custom types to easily incorporate complex64 pointer helper methods. |
| 236 | + |
| 237 | +## Contributing |
| 238 | + |
| 239 | +Contributions are welcome! Please open an issue or submit a pull request with any improvements or bug fixes. |
| 240 | + |
| 241 | +## License |
| 242 | + |
| 243 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 244 | + |
| 245 | +--- |
| 246 | + |
| 247 | +This README now includes information about the `Complex64Helper` struct and its usage. Feel free to customize it further based on your specific requirements. |
0 commit comments