dd_01_hello_world - basic bind/unbind
this example demonstrates the fundamental dd (dynamic data) operations: converting between structured data (map[string]any) and go structs using dd.New[T](), dd.Bind(), and dd.Unbind(). this is the "hello world" introduction to the dd package.
key concepts
- New[T] API: type-safe allocation using go generics
- Bind API: manual allocation control for advanced use cases
- bidirectional data mapping: convert from
map[string]any to structs and back
- struct tags: use
df tags for custom field mapping and validation
- nested structures: handle complex nested data with pointers to structs
- round-trip compatibility: data maintains integrity through bind/unbind cycles
- error handling: validate required fields and handle binding errors gracefully
usage
go run main.go
data structure
the example works with this user profile data:
userData := map[string]any{
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"active": true,
"profile": map[string]any{
"bio": "Software developer",
"website": "https://johndoe.dev",
},
}
struct definitions
type User struct {
Name string `dd:"+required"` // required field
Email string // default field mapping
Age int // type conversion
Active bool // boolean handling
Profile *Profile // nested struct (snake_case: "profile")
}
type Profile struct {
Bio string
Website string
}
workflow demonstrated
- allocate and bind: use
dd.New[T]() for type-safe allocation and binding
- unbinding: convert go structs back to
map[string]any
- round-trip: verify data integrity through the complete cycle
- error handling: show validation behavior for missing required fields
- manual binding: show
dd.Bind() for cases requiring manual allocation control
this example showcases both struct binding patterns, providing the foundation for data persistence, API marshaling, and configuration management patterns.