util

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp

func Clamp[T constraints.Ordered](x, lo, hi T) T

Clamp returns x constrained within [lo:hi] range. If x compares less than lo, returns lo; otherwise if hi compares less that x, returns hi; otherwise returns v.

Example
fmt.Println(Clamp(500, 400, 600))
fmt.Println(Clamp(200, 400, 600))
fmt.Println(Clamp(800, 400, 600))

fmt.Println(Clamp(5*time.Second, 4*time.Second, 6*time.Second).Milliseconds())
fmt.Println(Clamp(2*time.Second, 4*time.Second, 6*time.Second).Milliseconds())
fmt.Println(Clamp(8*time.Second, 4*time.Second, 6*time.Second).Milliseconds())

fmt.Println(Clamp(1.5, 1.4, 1.8))
fmt.Println(Clamp(1.5, 1.8, 1.8))
fmt.Println(Clamp(1.5, 2.1, 1.9))
Output:

500
400
600
5000
4000
6000
1.5
1.8
2.1

func ClampFunc

func ClampFunc[T any](x, lo, hi T, less LessFn[T]) T

ClampFunc returns x constrained within [lo:hi] range using the less func. If x compares less than lo, returns lo; otherwise if hi compares less that x, returns hi; otherwise returns v.

Example
fmt.Println(ClampFunc(1.5, 1.4, 1.8, lessMagnitude))
fmt.Println(ClampFunc(1.5, 1.8, 1.8, lessMagnitude))
fmt.Println(ClampFunc(1.5, 2.1, 1.9, lessMagnitude))
fmt.Println(ClampFunc(-1.5, -1.4, -1.8, lessMagnitude))
fmt.Println(ClampFunc(-1.5, -1.8, -1.8, lessMagnitude))
fmt.Println(ClampFunc(-1.5, -2.1, -1.9, lessMagnitude))
fmt.Println(ClampFunc(1.5, -1.5, -1.5, lessMagnitude))
Output:

1.5
1.8
2.1
-1.5
-1.8
-2.1
1.5

func Compare

func Compare[T any](a, b T, less LessFn[T]) int

Compare uses a less function to determine the ordering of 'a' and 'b'. It returns: * -1 if a < b * 1 if a > b * 0 if a == b

func Equal

func Equal[T comparable](a, b T) bool

Equal wraps the '==' operator for comparable types.

func HashBytes

func HashBytes(b []byte) uint64

func HashInt

func HashInt(i int) uint64

func HashInt16

func HashInt16(i int16) uint64

func HashInt32

func HashInt32(i int32) uint64

func HashInt64

func HashInt64(i int64) uint64

func HashInt8

func HashInt8(i int8) uint64

func HashString

func HashString(s string) uint64

func HashUint

func HashUint(i uint) uint64

func HashUint16

func HashUint16(u uint16) uint64

func HashUint32

func HashUint32(u uint32) uint64

func HashUint64

func HashUint64(u uint64) uint64

func HashUint8

func HashUint8(u uint8) uint64

func Less

func Less[T constraints.Ordered](a, b T) bool

Less wraps the '<' operator for ordered types.

func Max

func Max[T constraints.Ordered](a, b T) T

Max returns the max of a and b.

Example
fmt.Println(Max(7, 3))
fmt.Println(Max(2*time.Second, 3*time.Second).Milliseconds())
Output:

7
3000

func MaxFunc

func MaxFunc[T any](a, b T, less LessFn[T]) T

MaxFunc returns the max of a and b using the less func.

Example
fmt.Println(MaxFunc(2.5, -3.1, lessMagnitude))
Output:

-3.1

func Min

func Min[T constraints.Ordered](a, b T) T

Min returns the min of a and b.

Example
fmt.Println(Min(7, 3))
fmt.Println(Min(2*time.Second, 3*time.Second).Milliseconds())
Output:

3
2000

func MinFunc

func MinFunc[T any](a, b T, less LessFn[T]) T

MinFunc returns the min of a and b using the less func.

Example
fmt.Println(MinFunc(2.5, -3.1, lessMagnitude))
Output:

2.5

Types

type Comparator

type Comparator[T any] func(x, y T) int

type EqualFn

type EqualFn[T any] func(a, b T) bool

EqualFn is a function that returns whether 'a' and 'b' are equal.

type HashFn

type HashFn[T any] func(t T) uint64

HashFn is a function that returns the hash of 't'.

type LessFn

type LessFn[T any] func(a, b T) bool

LessFn is a function that returns whether 'a' is less than 'b'.

Jump to

Keyboard shortcuts

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