Example Usage
This directory contains a comprehensive set of examples demonstrating how to use the OrderedObject library. The examples cover both basic and advanced features, with a focus on practical use cases.
Running the Examples
Each example is a standalone executable program that can be run directly:
# Run basic operations example
go run basic/main.go
# Run JSON operations example
go run json/main.go
# Run Map operations example
go run map/main.go
# Run type safety example
go run type/main.go
# Run nested structures example
go run nested/main.go
# Run array operations example
go run array/main.go
# Run error handling example
go run error/main.go
Example Structure
The examples are organized into the following functional categories:
Core Features
-
Basic Operations (basic/main.go)
- Creating and populating objects
- Getting and updating values
- Deleting keys
- Iterating through entries
- Cloning objects
- Configuration management example
-
JSON Operations (json/main.go)
- Converting objects to JSON (three methods)
- Parsing JSON into objects
- User information handling
- JSON serialization best practices
-
Map Operations (map/main.go)
- Creating objects from maps
- Converting objects to maps
- Application settings management
- Order preservation demonstration
-
Type Safety (type/main.go)
- Using concrete types with generics
- Type-safe value retrieval
- Multiple type handling
- Struct integration
Advanced Features
-
Nested Structures (nested/main.go)
- Complex object hierarchies
- Application configuration management
- Nested value access
- JSON serialization of nested objects
-
Array Operations (array/main.go)
- Arrays of objects
- Order management
- Array element access
- Settings item processing example
-
Error Handling (error/main.go)
- JSON parsing errors
- Type assertion
- Safe value access
- Error recovery patterns
Key Concepts Demonstrated
The examples illustrate several important concepts:
-
Order Preservation
- Key insertion order is maintained
- JSON serialization preserves order
- Map conversion order handling
-
Type Safety
- Generic type parameters
- Type assertions
- Struct integration
- Type-safe value access
-
Method Chaining
- Fluent interface pattern
- Builder-style API
- Immutable operations
-
Error Handling
- JSON parsing
- Type conversion
- Safe value access
- Error recovery
Best Practices
The examples demonstrate several best practices:
-
Configuration Management
- Using ordered objects for config
- Environment-specific settings
- Configuration cloning
-
Data Processing
- Type-safe data handling
- Nested structure management
- Array operations
- Error handling
-
JSON Handling
- Multiple serialization methods
- Order preservation
- Nested object handling
- Error recovery
Expected Output
Each example includes clear output demonstrating:
- Object structure
- JSON serialization
- Order preservation
- Type safety
- Error handling
The output is formatted for clarity and includes comments explaining the results.
Notes
- All examples use the latest features of the library
- Error handling is demonstrated where appropriate
- Type safety is emphasized throughout
- Real-world use cases are provided
- Code is thoroughly commented
- Output is clearly formatted
Contributing Examples
If you'd like to contribute additional examples:
- Follow the existing code style
- Include clear comments
- Demonstrate a specific feature or use case
- Add appropriate error handling
- Update this README if necessary