Documentation
¶
Overview ¶
Package defaults provides functionality for setting default values on struct fields using struct tags. It supports various data types including primitives, slices, nested structs, and pointers with automatic type conversion and recursive processing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDefaults ¶
SetDefaults sets default values for struct fields using the "default" struct tag. It recursively processes nested structs and handles various data types including strings, integers, floats, booleans, slices, and pointers.
The function only sets defaults for fields that have zero values, preserving any existing non-zero values.
Supported tag format: `default:"value"`
Examples:
type Config struct {
Port int `default:"8080"`
Host string `default:"localhost"`
Timeout time.Duration `default:"30s"`
Debug bool `default:"false"`
Tags []string `default:"tag1,tag2,tag3"`
}
Parameters:
- ptr: A pointer to a struct that should have default values applied
Returns:
- error: An error if the operation fails, nil otherwise
Types ¶
This section is empty.