Skip to content
Node: ptr ?? fallbackgithub.com/sahilkhaire/gox/cond

CoalescePtr

Overview

CoalescePtr returns the first non-nil pointer's value, or zero if all nil.

If you are coming from Node.js, the closest pattern is ptr ?? fallback.

Signature

go
func CoalescePtr[T any](vals ...*T) T

Compare: Node.js · Standard Go · gox

js
const name = user?.name ?? 'guest';
go
name := "guest"
if user != nil {
    name = user.Name
}
go
import "github.com/sahilkhaire/gox/cond"

a, b := 1, 2

Example

go
import "github.com/sahilkhaire/gox/cond"

a, b := 1, 2

Tips

Prefer explicit zero-value checks in performance-critical hot paths if the compiler cannot prove types.

Standard library alternative

Use the standard library directly:

go
name := "guest"
if user != nil {
    name = user.Name
}

Back to cond package overview

MIT Licensed · Built for Node.js developers moving to Go