pointerhelpers

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2024 License: Apache-2.0 Imports: 0 Imported by: 1

README

pointerhelpers

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.

Installation

To install the pointerhelpers package, use the following command:

go get github.com/xorima/pointerhelpers

Usage

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:

Boolean
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
}
Integers
int
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
}
Other integer types

The package supports other integer types similarly: int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64.

Floating Point Numbers
float32
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
}
float64
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
}
Complex Numbers
complex64
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)
}
complex128
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)
}
Embedding Complex64Helper

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)
}
String
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: ""
}

Full API Reference

Functions
  • Bool(v bool) *bool
  • BoolValue(v *bool) bool
  • Int(v int) *int
  • IntValue(v *int) int
  • Int8(v int8) *int8
  • Int8Value(v *int8) int8
  • Int16(v int16) *int16
  • Int16Value(v *int16) int16
  • Int32(v int32) *int32
  • Int32Value(v *int32) int32
  • Int64(v int64) *int64
  • Int64Value(v *int64) int64
  • Uint(v uint) *uint
  • UintValue(v *uint) uint
  • Uint8(v uint8) *uint8
  • Uint8Value(v *uint8) uint8
  • Uint16(v uint16) *uint16
  • Uint16Value(v *uint16) uint16
  • Uint32(v uint32) *uint32
  • Uint32Value(v *uint32) uint32
  • Uint64(v uint64) *uint64
  • Uint64Value(v *uint64) uint64
  • Float32(v float32) *float32
  • Float32Value(v *float32) float32
  • Float64(v float64) *float64
  • Float64Value(v *float64) float64
  • Complex64(v complex64) *complex64
  • Complex64Value(v *complex64) complex64
  • Complex128(v complex128) *complex128
  • Complex128Value(v *complex128) complex128
  • String(v string) *string
  • StringValue(v *string) string
Structs
  • Complex64Helper

The Complex64Helper struct can be embedded into your custom types to easily incorporate complex64 pointer helper methods.

Contributing

Contributions are welcome! Please open an issue or submit a pull request with any improvements or bug fixes.

License

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool added in v1.2.0

func Bool(v bool) *bool

Bool returns a pointer to the Bool value passed in.

func BoolIsUnset added in v1.2.0

func BoolIsUnset(v *bool) bool

BoolIsUnset returns true if the pointer is nil

func BoolValue added in v1.2.0

func BoolValue(v *bool) bool

BoolValue returns the value of the bool pointer passed in or 0 if the pointer is nil.

func Complex128 added in v1.3.0

func Complex128(v complex128) *complex128

Complex128 returns a pointer to the complex128 value passed in.

func Complex128Value added in v1.3.0

func Complex128Value(v *complex128) complex128

Complex128Value returns the value of the complex128 pointer passed in or 0 if the pointer is nil.

func Complex64 added in v1.3.0

func Complex64(v complex64) *complex64

Complex64 returns a pointer to the complex64 value passed in.

func Complex64Value added in v1.3.0

func Complex64Value(v *complex64) complex64

Complex64Value returns the value of the complex64 pointer passed in or 0 if the pointer is nil.

func Float32 added in v1.3.0

func Float32(v float32) *float32

Float32 returns a pointer to the float32 value passed in.

func Float32Value added in v1.3.0

func Float32Value(v *float32) float32

Float32Value returns the value of the float32 pointer passed in or 0 if the pointer is nil.

func Float64 added in v1.3.0

func Float64(v float64) *float64

Float64 returns a pointer to the float64 value passed in.

func Float64Value added in v1.3.0

func Float64Value(v *float64) float64

Float64Value returns the value of the float64 pointer passed in or 0 if the pointer is nil.

func Int added in v1.3.0

func Int(v int) *int

Int returns a pointer to the int value passed in.

func Int16 added in v1.3.0

func Int16(v int16) *int16

Int16 returns a pointer to the int16 value passed in.

func Int16Value added in v1.3.0

func Int16Value(v *int16) int16

Int16Value returns the value of the int16 pointer passed in or 0 if the pointer is nil.

func Int32 added in v1.3.0

func Int32(v int32) *int32

Int32 returns a pointer to the int32 value passed in.

func Int32Value added in v1.3.0

func Int32Value(v *int32) int32

Int32Value returns the value of the int32 pointer passed in or 0 if the pointer is nil.

func Int64 added in v1.1.0

func Int64(v int64) *int64

Int64 returns a pointer to the int64 value passed in.

func Int64Value added in v1.1.0

func Int64Value(v *int64) int64

Int64Value returns the value of the int64 pointer passed in or 0 if the pointer is nil.

func Int8 added in v1.3.0

func Int8(v int8) *int8

Int8 returns a pointer to the int8 value passed in.

func Int8Value added in v1.3.0

func Int8Value(v *int8) int8

Int8Value returns the value of the int8 pointer passed in or 0 if the pointer is nil.

func IntValue added in v1.3.0

func IntValue(v *int) int

IntValue returns the value of the int pointer passed in or 0 if the pointer is nil.

func String

func String(v string) *string

String returns a pointer to the string value passed in.

func StringValue

func StringValue(v *string) string

StringValue returns the value of the string pointer passed in or "" if the pointer is nil.

func Uint added in v1.3.0

func Uint(v uint) *uint

Uint returns a pointer to the uint value passed in.

func Uint16 added in v1.3.0

func Uint16(v uint16) *uint16

Uint16 returns a pointer to the uint16 value passed in.

func Uint16Value added in v1.3.0

func Uint16Value(v *uint16) uint16

Uint16Value returns the value of the uint16 pointer passed in or 0 if the pointer is nil.

func Uint32 added in v1.3.0

func Uint32(v uint32) *uint32

Uint32 returns a pointer to the uint32 value passed in.

func Uint32Value added in v1.3.0

func Uint32Value(v *uint32) uint32

Uint32Value returns the value of the uint32 pointer passed in or 0 if the pointer is nil.

func Uint64 added in v1.3.0

func Uint64(v uint64) *uint64

Uint64 returns a pointer to the uint64 value passed in.

func Uint64Value added in v1.3.0

func Uint64Value(v *uint64) uint64

Uint64Value returns the value of the uint64 pointer passed in or 0 if the pointer is nil.

func Uint8 added in v1.3.0

func Uint8(v uint8) *uint8

Uint8 returns a pointer to the uint8 value passed in.

func Uint8Value added in v1.3.0

func Uint8Value(v *uint8) uint8

Uint8Value returns the value of the uint8 pointer passed in or 0 if the pointer is nil.

func UintValue added in v1.3.0

func UintValue(v *uint) uint

UintValue returns the value of the uint pointer passed in or 0 if the pointer is nil.

Types

type BoolHelper added in v1.2.0

type BoolHelper struct {
}

BoolHelper contains all Bool related pointer helpers

func (*BoolHelper) Bool added in v1.2.0

func (b *BoolHelper) Bool(v bool) *bool

Bool returns a pointer to the bool value passed in.

func (*BoolHelper) BoolIsUnset added in v1.2.0

func (b *BoolHelper) BoolIsUnset(v *bool) bool

BoolIsUnset returns true if the pointer is nil

func (*BoolHelper) BoolValue added in v1.2.0

func (b *BoolHelper) BoolValue(v *bool) bool

BoolValue returns the value of the bool pointer passed in or 0 if the pointer is nil.

type Complex128Helper added in v1.3.0

type Complex128Helper struct {
}

Complex128Helper contains all Complex128 related pointer helpers

func (*Complex128Helper) Complex128 added in v1.3.0

func (i *Complex128Helper) Complex128(v complex128) *complex128

Complex128 returns a pointer to the complex128 value passed in.

func (*Complex128Helper) Complex128Value added in v1.3.0

func (i *Complex128Helper) Complex128Value(v *complex128) complex128

Complex128Value returns the value of the complex128 pointer passed in or 0 if the pointer is nil.

type Complex64Helper added in v1.3.0

type Complex64Helper struct {
}

Complex64Helper contains all Complex64 related pointer helpers

func (*Complex64Helper) Complex64 added in v1.3.0

func (i *Complex64Helper) Complex64(v complex64) *complex64

Complex64 returns a pointer to the complex64 value passed in.

func (*Complex64Helper) Complex64Value added in v1.3.0

func (i *Complex64Helper) Complex64Value(v *complex64) complex64

Complex64Value returns the value of the complex64 pointer passed in or 0 if the pointer is nil.

type Float32Helper added in v1.3.0

type Float32Helper struct {
}

Float32Helper contains all Float32 related pointer helpers

func (*Float32Helper) Float32 added in v1.3.0

func (i *Float32Helper) Float32(v float32) *float32

Float32 returns a pointer to the float32 value passed in.

func (*Float32Helper) Float32Value added in v1.3.0

func (i *Float32Helper) Float32Value(v *float32) float32

Float32Value returns the value of the float32 pointer passed in or 0 if the pointer is nil.

type Float64Helper added in v1.3.0

type Float64Helper struct {
}

Float64Helper contains all Float64 related pointer helpers

func (*Float64Helper) Float64 added in v1.3.0

func (i *Float64Helper) Float64(v float64) *float64

Float64 returns a pointer to the float64 value passed in.

func (*Float64Helper) Float64Value added in v1.3.0

func (i *Float64Helper) Float64Value(v *float64) float64

Float64Value returns the value of the float64 pointer passed in or 0 if the pointer is nil.

type Int16Helper added in v1.3.0

type Int16Helper struct {
}

Int16Helper contains all Int16 related pointer helpers

func (*Int16Helper) Int16 added in v1.3.0

func (i *Int16Helper) Int16(v int16) *int16

Int16 returns a pointer to the int16 value passed in.

func (*Int16Helper) Int16Value added in v1.3.0

func (i *Int16Helper) Int16Value(v *int16) int16

Int16Value returns the value of the int16 pointer passed in or 0 if the pointer is nil.

type Int32Helper added in v1.3.0

type Int32Helper struct {
}

Int32Helper contains all Int32 related pointer helpers

func (*Int32Helper) Int32 added in v1.3.0

func (i *Int32Helper) Int32(v int32) *int32

Int32 returns a pointer to the int32 value passed in.

func (*Int32Helper) Int32Value added in v1.3.0

func (i *Int32Helper) Int32Value(v *int32) int32

Int32Value returns the value of the int32 pointer passed in or 0 if the pointer is nil.

type Int64Helper added in v1.1.0

type Int64Helper struct {
}

Int64Helper contains all Int64 related pointer helpers

func (*Int64Helper) Int64 added in v1.1.0

func (i *Int64Helper) Int64(v int64) *int64

Int64 returns a pointer to the int64 value passed in.

func (*Int64Helper) Int64Value added in v1.1.0

func (i *Int64Helper) Int64Value(v *int64) int64

Int64Value returns the value of the int64 pointer passed in or 0 if the pointer is nil.

type Int8Helper added in v1.3.0

type Int8Helper struct {
}

Int8Helper contains all Int8 related pointer helpers

func (*Int8Helper) Int8 added in v1.3.0

func (i *Int8Helper) Int8(v int8) *int8

Int8 returns a pointer to the int8 value passed in.

func (*Int8Helper) Int8Value added in v1.3.0

func (i *Int8Helper) Int8Value(v *int8) int8

Int8Value returns the value of the int8 pointer passed in or 0 if the pointer is nil.

type IntHelper added in v1.3.0

type IntHelper struct {
}

IntHelper contains all int related pointer helpers

func (*IntHelper) Int added in v1.3.0

func (i *IntHelper) Int(v int) *int

Int returns a pointer to the int value passed in.

func (*IntHelper) IntValue added in v1.3.0

func (i *IntHelper) IntValue(v *int) int

IntValue returns the value of the int pointer passed in or 0 if the pointer is nil.

type PointerHelper

type PointerHelper struct {
	StringHelper
	Int64Helper
	BoolHelper
}

PointerHelper is a struct to include all the various helpers in this module to enable easier composition outside of this package

type StringHelper

type StringHelper struct{}

StringHelper contains all String related pointer helpers

func (*StringHelper) String

func (s *StringHelper) String(v string) *string

String returns a pointer to the string value passed in.

func (*StringHelper) StringValue

func (s *StringHelper) StringValue(v *string) string

type Uint16Helper added in v1.3.0

type Uint16Helper struct {
}

Uint16Helper contains all Uint16 related pointer helpers

func (*Uint16Helper) Uint16 added in v1.3.0

func (i *Uint16Helper) Uint16(v uint16) *uint16

Uint16 returns a pointer to the uint16 value passed in.

func (*Uint16Helper) Uint16Value added in v1.3.0

func (i *Uint16Helper) Uint16Value(v *uint16) uint16

Uint16Value returns the value of the uint16 pointer passed in or 0 if the pointer is nil.

type Uint32Helper added in v1.3.0

type Uint32Helper struct {
}

Uint32Helper contains all Uint32 related pointer helpers

func (*Uint32Helper) Uint32 added in v1.3.0

func (i *Uint32Helper) Uint32(v uint32) *uint32

Uint32 returns a pointer to the uint32 value passed in.

func (*Uint32Helper) Uint32Value added in v1.3.0

func (i *Uint32Helper) Uint32Value(v *uint32) uint32

Uint32Value returns the value of the uint32 pointer passed in or 0 if the pointer is nil.

type Uint64Helper added in v1.3.0

type Uint64Helper struct {
}

Uint64Helper contains all Uint64 related pointer helpers

func (*Uint64Helper) Uint64 added in v1.3.0

func (i *Uint64Helper) Uint64(v uint64) *uint64

Uint64 returns a pointer to the uint64 value passed in.

func (*Uint64Helper) Uint64Value added in v1.3.0

func (i *Uint64Helper) Uint64Value(v *uint64) uint64

Uint64Value returns the value of the uint64 pointer passed in or 0 if the pointer is nil.

type Uint8Helper added in v1.3.0

type Uint8Helper struct {
}

Uint8Helper contains all Uint8 related pointer helpers

func (*Uint8Helper) Uint8 added in v1.3.0

func (i *Uint8Helper) Uint8(v uint8) *uint8

Uint8 returns a pointer to the uint8 value passed in.

func (*Uint8Helper) Uint8Value added in v1.3.0

func (i *Uint8Helper) Uint8Value(v *uint8) uint8

Uint8Value returns the value of the uint8 pointer passed in or 0 if the pointer is nil.

type UintHelper added in v1.3.0

type UintHelper struct {
}

UintHelper contains all Uint related pointer helpers

func (*UintHelper) Uint added in v1.3.0

func (i *UintHelper) Uint(v uint) *uint

Uint returns a pointer to the uint value passed in.

func (*UintHelper) UintValue added in v1.3.0

func (i *UintHelper) UintValue(v *uint) uint

UintValue returns the value of the uint pointer passed in or 0 if the pointer is nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL