Skip to content

Latest commit

 

History

History
20 lines (14 loc) · 790 Bytes

README.md

File metadata and controls

20 lines (14 loc) · 790 Bytes

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