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:=42pointerToVal:=&val// vs.pointerToVal:=pointy.Int(42) // if using Go 1.17 or earlier w/o genericspointerToVal:=pointy.Pointer(42) // if using Go 1.18+ w/ generics🚨 Breaking change
Package has changed to go.openly.dev. Please use
import "go.openly.dev/pointy"
Generic implementation of the pointer-to-value and value-to-pointer functions. Requires Go 1.18+. The type-specific functions are still available for backwards-compatibility.
pointerToInt:=pointy.Pointer(42) pointerToString:=pointy.Pointer("foo") // then later in your code..intValue:=pointy.PointerValue(pointerToInt, 99) stringValue:=pointy.PointerValue(pointerToString, "bar") Convenience functions to safely compare pointers by their dereferenced values:
// when both values are pointersa:=pointy.Int(1)
b:=pointy.Int(1)
ifpointy.PointersValueEqual(a, b) {
fmt.Println("a and b contain equal dereferenced values")
}
// or if just one is a pointera:=pointy.Int(1)
b:=1ifpointy.PointerValueEqual(a, b) {
fmt.Println("a and b contain equal dereferenced values")
}Additional helper functions have been added to safely dereference pointers or return a fallback value:
val:=42pointerToVal:=&val// then later in your code..myVal:=pointy.IntValue(pointerToVal, 99) // returns 42 (or 99 if pointerToVal was nil)https://godoc.org/github.com/openly-engineering/pointy
go get go.openly.dev/pointy
package main
import (
"fmt""go.openly.dev/pointy"
)
funcmain() {
foo:=pointy.Pointer(2018)
fmt.Println("foo is a pointer to:", *foo)
bar:=pointy.Pointer("point to me")
fmt.Println("bar is a pointer to:", *bar)
// get the value back out (new in v1.1.0)barVal:=pointy.PointerValue(bar, "empty!")
fmt.Println("bar's value is:", barVal)
}Pointer[T any](x T) *TPointerValue[T any](p *T, fallback T) TBool(x bool) *boolBoolValue(p *bool, fallback bool) boolByte(x byte) *byteByteValue(p *byte, fallback byte) byteComplex128(x complex128) *complex128Complex128Value(p *complex128, fallback complex128) complex128Complex64(x complex64) *complex64Complex64Value(p *complex64, fallback complex64) complex64Float32(x float32) *float32Float32Value(p *float32, fallback float32) float32Float64(x float64) *float64Float64Value(p *float64, fallback float64) float64Int(x int) *intIntValue(p *int, fallback int) intInt8(x int8) *int8Int8Value(p *int8, fallback int8) int8Int16(x int16) *int16Int16Value(p *int16, fallback int16) int16Int32(x int32) *int32Int32Value(p *int32, fallback int32) int32Int64(x int64) *int64Int64Value(p *int64, fallback int64) int64Uint(x uint) *uintUintValue(p *uint, fallback uint) uintUint8(x uint8) *uint8Uint8Value(p *uint8, fallback uint8) uint8Uint16(x uint16) *uint16Uint16Value(p *uint16, fallback uint16) uint16Uint32(x uint32) *uint32Uint32Value(p *uint32, fallback uint32) uint32Uint64(x uint64) *uint64Uint64Value(p *uint64, fallback uint64) uint64String(x string) *stringStringValue(p *string, fallback string) stringRune(x rune) *runeRuneValue(p *rune, fallback rune) runePointersValueEqual[T comparable](a *T, b *T) boolPointerValueEqual[T comparable](a *T, b T) bool
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 createlittleworkarounds. A common solution is to create a helper function:
funccreateInt64Pointer(xint64) *int64 {
return&x
}
// now you can create a pointer to 42 inlinevalue:=createInt64Pointer(42)This package provides a library of these simple little helper functions for every native Go primitive.
Made @ Openly. Join us and use Go to build cool stuff.