Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BlankStructure ¶ added in v2.2.5
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 ¶
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 ¶
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.