Copier copies values between Go structs, slices, and maps using matched field names, with struct tags for overrides, ignores, and required fields.
Overview
Copier is a Go package for copying data between types with similar shapes. It maps fields and methods by name, and can copy from structs to slices, slices to slices, or maps to maps.
Struct tags control behavior: copier:"-" skips fields, copier:"must" enforces copying, copier:"override" forces values even when IgnoreEmpty is set, and custom tag names align differently named fields.
The API suits conversions between struct layers, API boundaries, and tests where manual assignment would be repetitive. Errors or panics depend on tag combinations such as must versus must,nopanic.
Installation is via go get, and the module remains a focused utility without external services or CLI components.
Key features
- Struct-to-struct copying
- Slice and map conversions
- Tag-driven field rules
- Method-to-field mapping
- Nested structure support
Use cases
- Mapping API models to DTOs
- Duplicating configuration structs
- Test fixture cloning
- Layered service transformations