Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

typeutil source pure

Package typeutil provides utility functions for converting between different types and checking their states. It aims...

Overview

Package typeutil provides utility functions for converting between different types and checking their states. It aims to provide consistent behavior across different types while remaining lightweight and dependency-free.

Functions 7

func IsZero

1func IsZero(val any) bool
source

IsZero returns true if the value represents a "zero" or "empty" state for its type. For example:

  • Numbers: 0
  • Strings: ""
  • Slices/Maps: empty
  • nil: true
  • bool: false
  • time.Time: IsZero()
  • address: empty string

func ToBool

1func ToBool(val any) bool
source

ToBool converts any value to a boolean based on common programming conventions. For example:

  • Numbers: 0 is false, any other number is true
  • Strings: "", "0", "false", "f", "no", "n", "off" are false, others are true
  • Slices/Maps: empty is false, non-empty is true
  • nil: always false
  • bool: direct value

func ToInterfaceSlice

1func ToInterfaceSlice(val any) []any
source

ToInterfaceSlice converts various slice types to []any

func ToMapIntInterface

1func ToMapIntInterface(m any) (map[int]any, error)
source

ToMapIntInterface converts a map with int keys and any value type to map[int]any

func ToMapStringInterface

1func ToMapStringInterface(m any) (map[string]any, error)
source

ToMapStringInterface converts a map with string keys and any value type to map[string]any

func ToString

1func ToString(val any) string
source

ToString converts any value to its string representation. It supports a wide range of Go types including:

  • Basic: string, bool
  • Numbers: int, int8-64, uint, uint8-64, float32, float64
  • Special: time.Time, address, []byte
  • Slices: []T for most basic types
  • Maps: map[string]string, map[string]any
  • Interface: types implementing String() string

Example usage:

Example
1str := typeutil.ToString(42)               // "42"
2str = typeutil.ToString([]int{1, 2})      // "[1 2]"
3str = typeutil.ToString(map[string]string{ // "map[a:1 b:2]"
4    "a": "1",
5    "b": "2",
6})

func ToStringSlice

1func ToStringSlice(val any) []string
source

ToStringSlice converts various slice types to []string

Imports 5

  • errors stdlib
  • sort stdlib
  • strconv stdlib
  • strings stdlib
  • time stdlib

Source Files 3