structure

package
v2.2.5 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlankStructure added in v2.2.5

func BlankStructure(v any) error

BlankStructure resets the fields of a struct to default values, where strings become "string", numeric types are zeroed, slices/arrays become empty, and maps are zeroed. For nested structs, it recursively applies the same rules.

The function works with both struct values and pointers to structs. It preserves the structure of nested objects while resetting their values.

Supported types:

  • Strings (set to "string")
  • Integers (all sizes, set to 0)
  • Floats (all sizes, set to 0)
  • Booleans (set to false)
  • Complex numbers (set to 0+0i)
  • Maps (set to nil)
  • Slices (set to empty slice)
  • Arrays (zeroed)
  • Structs (recursively processed)
  • Pointers to structs (recursively processed if non-nil)

Parameters:

  • v: any - the struct or pointer to struct to be processed

Returns:

  • error - returns nil on success, or an error if:
  • the input is nil
  • the input is not a struct or pointer to struct
  • encounters an unhandled type

Example:

type Person struct {
    Name    string
    Age     int
    Address *struct {
        Street string
        City   string
    }
}

p := Person{
    Name: "John",
    Age:  30,
    Address: &struct {
        Street string
        City   string
    }{
        Street: "123 Main St",
        City:   "Anytown",
    },
}

err := BlankStructure(&p)
// After: p.Name = "string", p.Age = 0, p.Address.Street = "string", p.Address.City = "string"

func CopyExportedFields

func CopyExportedFields(dst, src any) error

CopyExportedFields copies all exported fields from src to dst, regardless of their current values. Useful for copying full configurations or cloning objects.

Note: Unexported fields (lowercase) will be ignored.

func MergeZeroFields

func MergeZeroFields(dst, src any)

MergeZeroFields copies non-zero fields from src to dst, but only for fields in dst that are currently zero. dst and src must be pointers to structs of the same type.

Example:

MergeZeroFields(&userConfig, &defaultConfig)

Only fields in userConfig that are zero-valued will be replaced by corresponding values from defaultConfig.

func StructToMap

func StructToMap(input any) map[string]any

StructToMap converts an exported struct (or a pointer to one) into a map[string]any. Only exported fields will be included in the map.

Useful for logging, serializing, or dynamic field access.

Example:

m := StructToMap(user)
fmt.Println(m["Name"], m["Active"])

func ZeroStruct

func ZeroStruct(v any)

ZeroStruct resets all settable fields in a struct to their zero value. Takes a pointer to a struct.

Example:

ZeroStruct(&config) // config.Name = "", config.Count = 0, config.Enabled = false, etc.

Types

This section is empty.

Jump to

Keyboard shortcuts

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