pointy

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2019 License: MIT Imports: 0 Imported by: 0

README

pointy

Simple helper functions to provide a shorthand to get a pointer to a variable holding a constant...because it's annoying when you have to do it hundreds of times in unit tests:

val := 42
pointerToVal := &val
// vs.
pointerToVal := pointy.Int(42)

New in release 1.1.0

Additional helper functions have been added to safely dereference pointers or return a fallback value:

val := 42
pointerToVal := &val
// then later in your code..
myVal := pointy.IntValue(pointerToVal, 99) // returns 42 (or 99 if pointerToVal was nil)

GoDoc

https://godoc.org/github.com/mwielbut/pointy

Installation

go get github.com/mwielbut/pointy

Example

package main

import (
	"fmt"

	"github.com/mwielbut/pointy"
)

func main() {
	foo := pointy.Int64(2018)
	fmt.Println("foo is a pointer to:", *foo)

	bar := pointy.String("point to me")
	fmt.Println("bar is a pointer to:", *bar)

	// get the value back out (new in v1.1.0)
	barVal := pointy.StringValue(bar, "empty!")
	fmt.Println("bar's value is:", barVal)
}

Available Functions

Bool(x bool) *bool BoolValue(p *bool, fallback bool) bool Byte(x byte) *byte ByteValue(p *byte, fallback byte) byte Complex128(x complex128) *complex128 Complex128Value(p *complex128, fallback complex128) complex128 Complex64(x complex64) *complex64 Complex64Value(p *complex64, fallback complex64) complex64 Float32(x float32) *float32 Float32Value(p *float32, fallback float32) float32 Float64(x float64) *float64 Float64Value(p *float64, fallback float64) float64 Int(x int) *int IntValue(p *int, fallback int) int Int8(x int8) *int8 Int8Value(p *int8, fallback int8) int8 Int16(x int16) *int16 Int16Value(p *int16, fallback int16) int16 Int32(x int32) *int32 Int32Value(p *int32, fallback int32) int32 Int64(x int64) *int64 Int64Value(p *int64, fallback int64) int64 Uint(x uint) *uint UintValue(p *uint, fallback uint) uint Uint8(x uint8) *uint8 Uint8Value(p *uint8, fallback uint8) uint8 Uint16(x uint16) *uint16 Uint16Value(p *uint16, fallback uint16) uint16 Uint32(x uint32) *uint32 Uint32Value(p *uint32, fallback uint32) uint32 Uint64(x uint64) *uint64 Uint64Value(p *uint64, fallback uint64) uint64 String(x string) *string StringValue(p *string, fallback string) string Rune(x rune) *rune RuneValue(p *rune, fallback rune) rune

Motivation

Creating pointers to literal constant values is useful, especially in unit tests. Go doesn't support simply using the address operator (&) to reference the location of e.g. value := &int64(42) so we're forced to create little workarounds. A common solution is to create a helper function:

func createInt64Pointer(x int64) *int64 {
    return &x
}
// now you can create a pointer to 42 inline
value := createInt64Pointer(42)

This package provides a library of these simple little helper functions for every native Go primitive.

Documentation

Overview

Package pointy is a set of simple helper functions to provide a shorthand to get a pointer to a variable holding a constant.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(x bool) *bool

Bool returns a pointer to a variable holding the supplied bool constant

func BoolValue added in v1.1.0

func BoolValue(p *bool, fallback bool) bool

BoolValue returns the bool value pointed to by p or fallback if p is nil

func Byte

func Byte(x byte) *byte

Byte returns a pointer to a variable holding the supplied byte constant

func ByteValue added in v1.1.0

func ByteValue(p *byte, fallback byte) byte

ByteValue returns the byte value pointed to by p or fallback if p is nil

func Complex128

func Complex128(x complex128) *complex128

Complex128 returns a pointer to a variable holding the supplied complex128 constant

func Complex128Value added in v1.1.0

func Complex128Value(p *complex128, fallback complex128) complex128

Complex128Value returns the complex128 value pointed to by p or fallback if p is nil

func Complex64

func Complex64(x complex64) *complex64

Complex64 returns a pointer to a variable holding the supplied complex64 constant

func Complex64Value added in v1.1.0

func Complex64Value(p *complex64, fallback complex64) complex64

Complex64Value returns the complex64 value pointed to by p or fallback if p is nil

func Float32

func Float32(x float32) *float32

Float32 returns a pointer to a variable holding the supplied float32 constant

func Float32Value added in v1.1.0

func Float32Value(p *float32, fallback float32) float32

Float32Value returns the float32 value pointed to by p or fallback if p is nil

func Float64

func Float64(x float64) *float64

Float64 returns a pointer to a variable holding the supplied float64 constant

func Float64Value added in v1.1.0

func Float64Value(p *float64, fallback float64) float64

Float64Value returns the float64 value pointed to by p or fallback if p is nil

func Int

func Int(x int) *int

Int returns a pointer to a variable holding the supplied int constant

func Int16

func Int16(x int16) *int16

Int16 returns a pointer to a variable holding the supplied int16 constant

func Int16Value added in v1.1.0

func Int16Value(p *int16, fallback int16) int16

Int16Value returns the int16 value pointed to by p or fallback if p is nil

func Int32

func Int32(x int32) *int32

Int32 returns a pointer to a variable holding the supplied int32 constant

func Int32Value added in v1.1.0

func Int32Value(p *int32, fallback int32) int32

Int32Value returns the int32 value pointed to by p or fallback if p is nil

func Int64

func Int64(x int64) *int64

Int64 returns a pointer to a variable holding the supplied int64 constant

Example

This example returns a pointer to a variable holding the `int64` constant `2018`.

package main

import (
	"fmt"

	"github.com/mwielbut/pointy"
)

func main() {
	foo := pointy.Int64(2018)
	fmt.Println("foo contains value:", *foo)
}
Output:

func Int64Value added in v1.1.0

func Int64Value(p *int64, fallback int64) int64

Int64Value returns the int64 value pointed to by p or fallback if p is nil

func Int8

func Int8(x int8) *int8

Int8 returns a pointer to a variable holding the supplied int8 constant

func Int8Value added in v1.1.0

func Int8Value(p *int8, fallback int8) int8

Int8Value returns the int8 value pointed to by p or fallback if p is nil

func IntValue added in v1.1.0

func IntValue(p *int, fallback int) int

IntValue returns the int value pointed to by p or fallback if p is nil

func Rune

func Rune(x rune) *rune

Rune returns a pointer to a variable holding the supplied rune constant

func RuneValue added in v1.1.0

func RuneValue(p *rune, fallback rune) rune

RuneValue returns the rune value pointed to by p or fallback if p is nil

func String

func String(x string) *string

String returns a pointer to a variable holding the supplied string constant

Example

This example returns a pointer to a variable holding the `string` constant "point to me".

package main

import (
	"fmt"

	"github.com/mwielbut/pointy"
)

func main() {
	bar := pointy.String("point to me")
	fmt.Println("bar contains value:", *bar)
}
Output:

func StringValue added in v1.1.0

func StringValue(p *string, fallback string) string

StringValue returns the string value pointed to by p or fallback if p is nil

func Uint

func Uint(x uint) *uint

Uint returns a pointer to a variable holding the supplied uint constant

func Uint16

func Uint16(x uint16) *uint16

Uint16 returns a pointer to a variable holding the supplied uint16 constant

func Uint16Value added in v1.1.0

func Uint16Value(p *uint16, fallback uint16) uint16

Uint16Value returns the uint16 value pointed to by p or fallback if p is nil

func Uint32

func Uint32(x uint32) *uint32

Uint32 returns a pointer to a variable holding the supplied uint32 constant

func Uint32Value added in v1.1.0

func Uint32Value(p *uint32, fallback uint32) uint32

Uint32Value returns the uint32 value pointed to by p or fallback if p is nil

func Uint64

func Uint64(x uint64) *uint64

Uint64 returns a pointer to a variable holding the supplied uint64 constant

func Uint64Value added in v1.1.0

func Uint64Value(p *uint64, fallback uint64) uint64

Uint64Value returns the uint64 value pointed to by p or fallback if p is nil

func Uint8

func Uint8(x uint8) *uint8

Uint8 returns a pointer to a variable holding the supplied uint8 constant

func Uint8Value added in v1.1.0

func Uint8Value(p *uint8, fallback uint8) uint8

Uint8Value returns the uint8 value pointed to by p or fallback if p is nil

func UintValue added in v1.1.0

func UintValue(p *uint, fallback uint) uint

UintValue returns the uint value pointed to by p or fallback if p is nil

Types

This section is empty.

Jump to

Keyboard shortcuts

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