typeutil

package
v1.157.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 1 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

typeutil

Go Reference

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.

Example

var nilChan chan int

v := typeutil.IsNil(nilChan)
fmt.Println(v)

// Output:
// true

Full source is in example_typeutil_test.go. More runnable examples are on pkg.go.dev.

Dependencies

This package reaches no external module: it uses only the Go standard library.

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

func BoolToInt(b bool) int

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

func BoolToNum[T Number](b bool) T

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

func IsNil(v any) bool

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

func IsZero[T any](v T) bool

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

Types

type Float

type Float interface {
	~float32 | ~float64
}

Float is a constraint for float types.

type Int

type Int interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Int is a constraint for signed integer types.

type Number

type Number interface {
	Int | UInt | Float
}

Number is a constraint for all integer and float numbers.

type Ordered

type Ordered interface {
	Number | ~string
}

Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >.

It matches the standard library's cmp.Ordered; it is kept here so consumers can use a single constraints vocabulary alongside Number.

type UInt

type UInt interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

UInt is a constraint for unsigned integer types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL