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.
To install the pointerhelpers package, use the following command:
go get github.com/xorima/pointerhelpersThe pointerhelpers package provides functions to obtain pointers to values and to safely retrieve values from pointers. Below are the examples for each supported type:
package main
import (
"fmt"
"github.com/xorima/pointerhelpers"
)
func main() {
b := true
bPtr := pointerhelpers.Bool(b)
fmt.Println(*bPtr) // Output: true
nilBoolPtr := (*bool)(nil)
fmt.Println(pointerhelpers.BoolValue(nilBoolPtr)) // Output: false
}package main
import (
"fmt"
"github.com/xorima/pointerhelpers"
)
func main() {
i := 42
iPtr := pointerhelpers.Int(i)
fmt.Println(*iPtr) // Output: 42
nilIntPtr := (*int)(nil)
fmt.Println(pointerhelpers.IntValue(nilIntPtr)) // Output: 0
}The package supports other integer types similarly: int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64.
package main
import (
"fmt"
"github.com/xorima/pointerhelpers"
)
func main() {
f := float32(3.14)
fPtr := pointerhelpers.Float32(f)
fmt.Println(*fPtr) // Output: 3.14
nilFloat32Ptr := (*float32)(nil)
fmt.Println(pointerhelpers.Float32Value(nilFloat32Ptr)) // Output: 0
}package main
import (
"fmt"
"github.com/xorima/pointerhelpers"
)
func main() {
f := 3.14
fPtr := pointerhelpers.Float64(f)
fmt.Println(*fPtr) // Output: 3.14
nilFloat64Ptr := (*float64)(nil)
fmt.Println(pointerhelpers.Float64Value(nilFloat64Ptr)) // Output: 0
}package main
import (
"fmt"
"github.com/xorima/pointerhelpers"
)
func main() {
c := complex64(1 + 2i)
cPtr := pointerhelpers.Complex64(c)
fmt.Println(*cPtr) // Output: (1+2i)
nilComplex64Ptr := (*complex64)(nil)
fmt.Println(pointerhelpers.Complex64Value(nilComplex64Ptr)) // Output: (0+0i)
}package main
import (
"fmt"
"github.com/xorima/pointerhelpers"
)
func main() {
c := complex(1 + 2i)
cPtr := pointerhelpers.Complex128(c)
fmt.Println(*cPtr) // Output: (1+2i)
nilComplex128Ptr := (*complex128)(nil)
fmt.Println(pointerhelpers.Complex128Value(nilComplex128Ptr)) // Output: (0+0i)
}For users who want to embed Complex64Helper into their custom types to gain the pointer helper functionalities directly, the package provides the Complex64Helper struct.
package main
import (
"fmt"
"github.com/xorima/pointerhelpers"
)
type MyStruct struct {
pointerhelpers.Complex64Helper
}
func main() {
s := MyStruct{}
c := complex64(1 + 2i)
cPtr := s.Complex64(c)
fmt.Println(*cPtr) // Output: (1+2i)
nilComplex64Ptr := (*complex64)(nil)
fmt.Println(s.Complex64Value(nilComplex64Ptr)) // Output: (0+0i)
}package main
import (
"fmt"
"github.com/xorima/pointerhelpers"
)
func main() {
s := "hello"
sPtr := pointerhelpers.String(s)
fmt.Println(*sPtr) // Output: hello
nilStringPtr := (*string)(nil)
fmt.Println(pointerhelpers.StringValue(nilStringPtr)) // Output: ""
}Bool(v bool) *boolBoolValue(v *bool) boolInt(v int) *intIntValue(v *int) intInt8(v int8) *int8Int8Value(v *int8) int8Int16(v int16) *int16Int16Value(v *int16) int16Int32(v int32) *int32Int32Value(v *int32) int32Int64(v int64) *int64Int64Value(v *int64) int64Uint(v uint) *uintUintValue(v *uint) uintUint8(v uint8) *uint8Uint8Value(v *uint8) uint8Uint16(v uint16) *uint16Uint16Value(v *uint16) uint16Uint32(v uint32) *uint32Uint32Value(v *uint32) uint32Uint64(v uint64) *uint64Uint64Value(v *uint64) uint64Float32(v float32) *float32Float32Value(v *float32) float32Float64(v float64) *float64Float64Value(v *float64) float64Complex64(v complex64) *complex64Complex64Value(v *complex64) complex64Complex128(v complex128) *complex128Complex128Value(v *complex128) complex128String(v string) *stringStringValue(v *string) string
Complex64Helper
The Complex64Helper struct can be embedded into your custom types to easily incorporate complex64 pointer helper methods.
Contributions are welcome! Please open an issue or submit a pull request with any improvements or bug fixes.
This project is licensed under the MIT License - see the LICENSE file for details.
This README now includes information about the Complex64Helper struct and its usage. Feel free to customize it further based on your specific requirements.