anyutil

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 13 Imported by: ? 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

Index

Constants

This section is empty.

Variables

View Source
var (
	// Conversion failure for a value with no numeric form: a type that is not a number, text that does not parse, NaN asked for as an integer
	ErrNotNumber = errors.New("not a number")
	// Conversion failure for a number the target cannot hold: infinity or beyond the int64 range for an integer, beyond float64 for a float; the strconv sentinel
	ErrRange = strconv.ErrRange
)

Functions

func Clone

func Clone[T any](v T) T

Deep copy of a value through maps, slices, arrays, pointers, objects and exported struct fields, keeping shared references shared and cycles intact

func Equal

func Equal(a, b any) bool

Whether two values are deeply equal: numbers by value across integer and float types, a bool only to a bool, sequences element by element, maps and objects by their entries whatever their key order (an object being a *jsonutil.Object or any value that lists its entries through Len, Get and All), values that contain themselves without looping

func Float

func Float(v any) (float64, bool)

Number a value holds when its type is a Go integer or float kind, a json.Number or a *big.Int

func Int

func Int(v any) (int64, bool)

Integer a value holds when its type is a Go integer kind, an integral json.Number or a *big.Int within int64

func IsTrue

func IsTrue(v any) bool

Whether a value counts as true the way Go templates test it: false for nil, false, zero numbers, empty strings, slices, maps and objects, nil pointers and anything whose Len is 0

func Kind

func Kind(v any) string

Word for the type of a value in messages: "null", "boolean", "integer", "number", "string", "bytes", "array", "object", else the Go type

func Or

func Or(vals ...any) any

First value that is true, else the last one, nil when there are none

func Quote

func Quote(v any) string

A string, a string-kind value or []byte in Go double-quoted form, anything else as String gives it

func String

func String(v any) string

Text of a value: "" for nil, a string as is, []byte as text, numbers in decimal, true or false, an error's or Stringer's own text, containers as compact JSON

func StringOr

func StringOr(v any, def string) string

Text of a value that counts as true, as String gives it, def for one that does not

func ToFloat

func ToFloat(v any) (float64, error)

Float of a number, true and false as 1 and 0, or numeric text around which space is ignored; errors wrap ErrNotNumber (result 0) or ErrRange (result the infinity of the value's sign, as strconv.ParseFloat gives it)

func ToInt

func ToInt(v any) (int64, error)

Integer of a number, floats cut toward zero, true and false as 1 and 0, or decimal text around which space is ignored; errors wrap ErrNotNumber (result 0) or ErrRange (result math.MaxInt64 or math.MinInt64 by the sign of the value, as strconv.ParseInt gives it)

Types

This section is empty.

Jump to

Keyboard shortcuts

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