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) TCompare: 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, 2Example
go
import "github.com/sahilkhaire/gox/cond"
a, b := 1, 2Tips
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
}