Skip to content

earthboundkid/pointer

Repository files navigation

pointer Go Reference

Some generic Go pointer helpers:

// Start with a null pointer
var strptr *string

// pointer.Deref safely dereferences a nil pointer into its empty value
fmt.Println(pointer.Deref(strptr) == "") // prints true

// pointer.Coalesce lets us specify a default value for a nil pointer
fmt.Println(pointer.Coalesce(strptr, "hello, world")) // prints "hello, world"

// We can create a pointer to a string or other primitive type with pointer.New
newptr := pointer.New("meaning of life") // takes a pointer to a string, wow!

// pointer.First returns the first pointer that isn't nil.
strptr = pointer.First(strptr, newptr) // returns newptr

About

Generic pointer helpers

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages