Documentation
¶
Overview ¶
Package typeutil detects nil through interfaces, obtains zero values generically, dereferences pointers safely, and converts booleans to integers without a branch.
- IsNil: reflection-based nil check that handles all nilable kinds (chan, func, interface, map, pointer, slice, unsafe pointer) and the untyped nil case, including a nil concrete pointer wrapped in a non-nil interface that `v == nil` misses.
- IsZero: returns true when the value equals the zero value for its type (empty string, 0, nil pointer, false), without requiring a comparable constraint.
- Zero: returns the zero value for any type T.
- Value: dereferences a pointer, returning the zero value of T when the pointer is nil.
- BoolToInt: converts a bool to 0 or 1 using the pattern the Go compiler optimizes to a single MOVBLZX instruction.
Usage ¶
// Nil detection through an interface:
var p *MyStruct
var i any = p
typeutil.IsNil(i) // true, whereas i == nil is false
// Zero value inferred from an existing value, as a sentinel return:
func check[T any](v T) (T, error) {
if !valid(v) {
return typeutil.Zero(v), errInvalid
}
return v, nil
}
// Safe pointer dereference:
var timeout *time.Duration
d := typeutil.Value(timeout) // 0, no panic
// Branch-free bool-to-int:
score += typeutil.BoolToInt(isBonus) * bonusPoints
When To Use ¶
- Generic code must detect nil through an interface, where == nil is not enough.
- You need the zero value of a type parameter.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolToInt ¶
BoolToInt converts bool to 0 or 1 with compiler optimization to MOVBLZX; avoids branch in hot paths.
It delegates to BoolToNum; the int shape is inlined to the same single MOVBLZX instruction, so there is no cost over an open-coded version.
func BoolToNum ¶
BoolToNum converts bool to 0 or 1 of any numeric type T, saving a cast at numeric call sites; see BoolToInt for the int-specific version.
Integer instantiations keep the branch-free MOVBLZX codegen of BoolToInt; float instantiations compile to a small conditional load.
func IsNil ¶
IsNil reliably detects nil including nil pointers wrapped in non-nil interfaces; works on any nilable type.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/typeutil"
)
func main() {
var nilChan chan int
v := typeutil.IsNil(nilChan)
fmt.Println(v)
}
Output: true
func IsZero ¶
IsZero returns true if value equals type T's zero value without requiring comparable constraint.
It goes through reflection. The call is inlinable and normally allocates nothing, but if v (or its address) escapes at the call site it is heap-allocated; prefer a direct v == zero comparison on hot paths where T is a known comparable type.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/typeutil"
)
func main() {
var zeroInt int
v := typeutil.IsZero(zeroInt)
fmt.Println(v)
}
Output: true
func Value ¶
func Value[T any](p *T) T
Value safely dereferences pointer p, returning zero value of T if nil.
The nil check is a direct pointer comparison (no reflection), keeping this helper cheap on hot paths with frequent pointer dereferences.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/typeutil"
)
func main() {
num := 5
p := &num
v := typeutil.Value(p)
fmt.Println(v)
}
Output: 5
func Zero ¶
func Zero[T any](_ T) T
Zero returns zero value for type T as a generic sentinel, useful for readable error returns.
The argument exists only to infer T; it is evaluated but otherwise unused, so avoid passing an expensive or panic-prone expression.
Example ¶
package main
import (
"fmt"
"github.com/tecnickcom/nurago/pkg/typeutil"
)
func main() {
num := 5
v := typeutil.Zero(num)
fmt.Println(v)
}
Output: 0