typeutil

package
v1.114.2 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package typeutil contains a collection of type-related generic utility functions.

This package provides a set of utility functions and definitions for working with generic types in Go.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToInt

func BoolToInt(b bool) int

BoolToInt converts a boolean value to an integer.

NOTE: this is currently the fastest implementation as it will be optimized by the compiler with a MOVBLZX instruction. Ref.:

func IsNil

func IsNil(v any) bool

IsNil returns true if the input value is nil.

Example
package main

import (
	"fmt"

	"github.com/tecnickcom/gogen/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 the input value is equal to the zero instance (e.g. empty string, 0 int, nil pointer).

Example
package main

import (
	"fmt"

	"github.com/tecnickcom/gogen/pkg/typeutil"
)

func main() {
	var zeroInt int

	v := typeutil.IsZero(zeroInt)
	fmt.Println(v)

}
Output:

true

func Pointer

func Pointer[T any](v T) *T

Pointer returns the address of v.

Example
package main

import (
	"fmt"

	"github.com/tecnickcom/gogen/pkg/typeutil"
)

func main() {
	v := 5

	p := typeutil.Pointer(v)
	fmt.Println(p)
}

func Value

func Value[T any](p *T) T

Value returns the value of the provided pointer or the type default (zero value) if nil.

Example
package main

import (
	"fmt"

	"github.com/tecnickcom/gogen/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 the zero instance (e.g. empty string, 0 int, nil pointer).

Example
package main

import (
	"fmt"

	"github.com/tecnickcom/gogen/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 < <= >= >.

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