intconv

package
v0.3.11 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 5 Imported by: 6 modules, 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.

Documentation

Overview

Package intconv converts between integer types without silent wraparound: Exact refuses a value the target cannot hold, Must panics on one, Clamp saturates it, and Duration scales a count into a time.Duration. Uint32Bits, Uint64Bits, Int32Bits and Int64Bits reinterpret two's-complement bits, where a wire format means exactly that.

Index

Constants

This section is empty.

Variables

View Source
var ErrRange = errors.New("integer out of range")

ErrRange is what every *RangeError unwraps to, for errors.Is.

Functions

func Clamp

func Clamp[T, F Integer](v F) T

Clamp returns v as a T, saturated to T's range.

func Duration

func Duration[F Integer](n F, unit time.Duration) time.Duration

Duration is n units, saturated to time.Duration's range. It panics on a unit that is not positive.

func Exact

func Exact[T, F Integer](v F) (T, error)

Exact returns v as a T, or a *RangeError when T cannot hold it.

func Int32Bits

func Int32Bits(v uint32) int32

Int32Bits is the int32 with v's two's-complement bits, the value a wire format's signed 32-bit field carries in v: Uint32Bits's inverse.

func Int64Bits

func Int64Bits(v uint64) int64

Int64Bits is the int64 with v's two's-complement bits, the value a wire format's signed 64-bit field carries in v: Uint64Bits's inverse.

func Must

func Must[T, F Integer](v F) T

Must is v as T, for a value the caller has already bounded: a v T cannot hold is a bug in the caller, and panics with its *RangeError.

func Uint32Bits

func Uint32Bits(v int32) uint32

Uint32Bits is v's two's-complement bits, the word a reader taking it as an int32 finds v in.

func Uint64Bits

func Uint64Bits(v int64) uint64

Uint64Bits is v's two's-complement bits, the word a reader taking it as an int64 finds v in.

Types

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer is every Go integer type.

type RangeError

type RangeError struct {
	Value  string
	Target reflect.Type
}

RangeError is a value its target type cannot hold.

func (*RangeError) Error

func (e *RangeError) Error() string

func (*RangeError) Unwrap

func (e *RangeError) Unwrap() error

Unwrap is ErrRange.

Jump to

Keyboard shortcuts

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