dd_01_hello_world

command
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 3 Imported by: 0

README

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

  1. allocate and bind: use dd.New[T]() for type-safe allocation and binding
  2. unbinding: convert go structs back to map[string]any
  3. round-trip: verify data integrity through the complete cycle
  4. error handling: show validation behavior for missing required fields
  5. 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.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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