Documentation
¶
Overview ¶
Package mapx provides type-safe utility functions for working with Go maps.
The core generic functions ValueOf and ValueOr eliminate boilerplate for typed map access. Convenience wrappers (String, Bool, etc.) are provided for common types. Numeric helpers use explicit `Coerce` suffixes when they accept compatible numeric types.
Usage:
name, ok := mapx.ValueOf[string](m, "name") port := mapx.ValueOr(m, "port", 8080) merged := mapx.Merge(a, b)
Index ¶
- Variables
- func AnyOr(m map[string]any, key string, def any) any
- func AnySlice(m map[string]any, key string) ([]any, bool)
- func AnySliceOr(m map[string]any, key string, def []any) []any
- func Bool(m map[string]any, key string) (bool, bool)
- func BoolOr(m map[string]any, key string, def bool) bool
- func Copy(m map[string]any) map[string]any
- func Count(m map[string]any) int
- func Float64Coerce(m map[string]any, key string) (float64, bool)
- func Float64CoerceOr(m map[string]any, key string, def float64) float64
- func Get(m map[string]any, key string) (any, bool)
- func Has(m map[string]any, key string) bool
- func IntCoerce(m map[string]any, key string) (int, bool)
- func IntCoerceOr(m map[string]any, key string, def int) int
- func Keys(v any) []string
- func Map(m map[string]any, key string) (map[string]any, bool)
- func MapOr(m map[string]any, key string, def map[string]any) map[string]any
- func Merge(a, b map[string]any) map[string]any
- func Set(m map[string]any, key string, value any)
- func String(m map[string]any, key string) (string, bool)
- func StringOr(m map[string]any, key, def string) string
- func Strings(m map[string]any, key string) ([]string, bool)
- func StringsOr(m map[string]any, key string, def []string) []string
- func ToGeneric(v any) (map[any]any, error)
- func ValueOf[T any](m map[string]any, key string) (T, bool)
- func ValueOr[T any](m map[string]any, key string, def T) T
Constants ¶
This section is empty.
Variables ¶
var ErrInputNotMap = errors.New("input is not a map type")
ErrInputNotMap indicates that the input is not a map type.
Functions ¶
func AnySliceOr ¶ added in v0.6.0
AnySliceOr returns the []any value for key, or def.
func Float64Coerce ¶ added in v0.10.0
Float64Coerce returns the float64 value for key, with numeric coercion from float32, int, int32, and int64.
func Float64CoerceOr ¶ added in v0.10.0
Float64CoerceOr returns the float64 value for key (with numeric coercion), or def.
func IntCoerce ¶ added in v0.10.0
IntCoerce returns the int value for key, with numeric coercion from int32, int64, and float64.
func IntCoerceOr ¶ added in v0.10.0
IntCoerceOr returns the int value for key (with numeric coercion), or def.
func Keys ¶
Keys returns the string keys of an object (map or struct). For map[any]any, only string keys are included.
func Merge ¶
Merge returns a new map containing all entries from both maps. Entries in b take precedence over entries in a.
func ToGeneric ¶
ToGeneric converts any map type to map[any]any. It returns ErrInputNotMap for non-map inputs.
Types ¶
This section is empty.