jinzhu/copier: struct-to-struct copying in Go, and where it stops
Copier for golang, copy value from struct to struct and more
At a glance
- What is it?
- Copier is a small MIT-licensed Go package that copies values between structs, slices and maps by matching names. It is convenient for DTO mapping and thin for anything that needs strict validation.
- Who is it for?
- Adopt jinzhu/copier if you need name-based mapping between a small number of Go structs, slices or maps and you accept that mismatches are mostly silent. Do not adopt it if you need compile-time field mapping, automatic deep-clone semantics for shared pointers, or a mapping layer that fails loudly on every unexpected field.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Activity is slowing. The repository last received commits 8 months ago.
- What is it written in?
- Mainly Go, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 15, 2026, and from our analysis. They are not legal advice.
Star history
- jinzhu/copier6,173 ★ · -3
| Repository | 2026-09-23 | 2026-11-15 |
|---|---|---|
| jinzhu/copier | 6.2k | 6.2k |
-3 Stars in the last 53 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The problem jinzhu/copier solves, and the Go code it replaces
Go has no built-in way to copy one struct's fields into another struct of a different type. The language gives you assignment between identical types and conversion between types with the same underlying structure, and nothing else. In practice that means a hand-written function for every pair of types: one line per field, repeated for every DTO, entity and API model in the codebase. Copier replaces those functions with a single call that matches source and destination members by name.
The audience is narrow and specific. It is for Go developers who already have two struct shapes that mostly agree, and who want the mapping expressed as a convention rather than as code. The README's own example is a User with Name, Role and Age copied into an Employee with Name, Age, DoubleAge and SuperRole, where the destination also has a Role method. After the call, the printed result is Employee{Name:"Jinzhu", Age:18, DoubleAge:36, SuperRole:"Super Admin"}. Two of those four values came from methods, one on the source and one on the destination, not from plain field copies, which is the part a hand-written mapper would have needed extra lines for.
The library also covers containers. The README documents slice to slice, struct to slice, and map to map, including a map[int]int copied into a map[int32]int8. That last example is worth pausing on: the keys and values are converted to the destination's element types, so the copy is not a pure assignment. If your types line up exactly, you are paying for a reflection-based conversion you may not need.
How the copy actually works: name matching, methods and tags
Copier walks the destination and looks for a matching member on the source, then assigns through reflection. Matching is by name, and the README shows that methods participate: a source method DoubleAge() int32 fills a destination field DoubleAge, and a destination method Role(string) receives the source's Role field. That is the mechanism behind the SuperRole output in the basic example, where the destination method prefixes the value rather than storing it directly.
Tags change the matching rules per field. copier:"-" excludes a destination field, and the README's example shows Secret staying empty after the copy. copier:"must" forces a field to be copied and is documented to panic or return an error when it cannot be. copier:"must,nopanic" returns an error instead of panicking, which is the variant you want in a request path. copier:"override" makes a field copy even when the IgnoreEmpty option is set, and the README states it works for nil values, so an override field can be overwritten with nil. A tag can also name the source field explicitly, as in ID int64 `copier:"Identifier"`, which maps SourceEmployee.Identifier onto TargetWorker.ID.
The layout of the repository shows what is tested. The test files are split by concern: copier_case_insensitive_test.go, copier_different_type_test.go, copier_field_name_mapping_test.go, copier_converter_test.go, copier_tags_test.go, plus copier_issue84_test.go and copier_issue170_test.go. Case-insensitive matching and type conversion therefore have their own test surface.
Installing jinzhu/copier and copying your first struct
The README gives one installation step: install Go, then fetch the module. The go.mod file declares module github.com/jinzhu/copier with a go 1.13 directive, so the module targets that toolchain level.
go get -u github.com/jinzhu/copierAfter that, import the package. There is no command-line tool and no server process; it is a library you call from Go code.
import "github.com/jinzhu/copier"A first real use is the basic example from the README. Define a source with a method and a destination with a field and a method, then copy and print. The output line in the README is Employee{Name:"Jinzhu", Age:18, DoubleAge:36, SuperRole:"Super Admin"}, which confirms that both the field and the method paths were exercised in one call.
type User struct {
Name string
Role string
Age int32
}
func (user *User) DoubleAge() int32 {
return 2 * user.Age
}
func main() {
user := User{Name: "Jinzhu", Age: 18, Role: "Admin"}
employee := Employee{}
copier.Copy(&employee, &user)
}Note the pointer arguments. Every example in the README passes the destination by address, and the destination must be addressable for reflection to write into it. If you pass a destination by value, you are outside what the documentation shows.
Options are passed through CopyWithOption. The README's override example builds copier.Option{IgnoreEmpty: true} and calls CopyWithOption(&target, &source, copier.Option{IgnoreEmpty: true}). With a nil source pointer and an override-tagged destination pointer, the README states the destination field is overridden to nil. That is the behaviour to check first if you are using IgnoreEmpty to protect existing values.
Where jinzhu/copier is the wrong tool
The largest limitation is that a name mismatch is usually not an error. If the destination field is not tagged must, and the source has no member with that name, the copy simply leaves the field alone. In a mapping layer between an API request and a database entity, that means a renamed field can silently stop being populated, and the compiler will not tell you. The must tag exists precisely because this is a problem, but it is opt-in per field.
Pointer and nested struct handling is the second sharp edge. In the README's complex example, Employee has Addresses []Address and Contact *Contact, and Manager has a Contact *Contact tagged override. Whether a non-nil nested pointer is cloned or shared between source and destination is not shown in these examples. If it is shared, mutating the copy mutates the original, and that is a class of bug that is hard to find later; treat this as something to verify with your own types before relying on it.
Finally, this is not a validation library and not a mapping DSL. It does not generate code, so every mapping decision is resolved at runtime through reflection. If you need compile-time guarantees, a code generator that produces the assignment functions for you gives you errors at build time instead of at request time.
The real alternative: generated mappers instead of reflection
The closest alternative in Go is a code generator that reads your struct definitions and writes the field-by-field copy functions, then commits them to the repository. The difference in approach is where the failure happens. Copier resolves names at runtime, so a typo or a renamed field shows up as an empty value in production. A generator resolves them at build time, so the same mistake fails compilation.
The trade-off runs the other way too. A generator adds a build step and a generated file to review, and it needs a directive or comment on every type pair you want mapped. Copier needs no build step and no generated code: you add the import, call Copy, and the mapping is whatever the names say. For a handful of struct pairs that change rarely, that is a smaller amount of machinery. For a large domain model with dozens of mappings, the generator's compile-time checking is usually worth the extra step.
There is also a middle path worth naming: write the mapper by hand. It is boring, it is explicit, and it is the thing Copier is replacing. If your structs have fewer than roughly ten fields, the hand-written function is often shorter than the tag annotations you would add to make Copier behave predictably.
Maintenance, licence and the cost of upgrading
The repository is not archived, and the last push was on 2026-03-14. That is a real activity signal but a modest one: the repository has no published releases, so the way you consume it is by module version through the Go toolchain rather than by downloading a tagged artifact. Copier has no runtime dependencies of its own; go.mod contains only the module line and the go 1.13 directive, so pulling it in does not drag a dependency tree behind it.
The licence is MIT, which is permissive and places few obligations on how you redistribute or modify it. That is a statement about the licence identifier in the repository, not legal advice; if your organisation has a policy on third-party licences, the MIT text in the repository's License file is what your review process will read.
The upgrade cost is mostly behavioural, not structural. Because the package resolves names at runtime, a change in matching rules or tag handling would not break your build. It would change what gets copied. That means the thing to pin is the module version in go.mod, and the thing to test after any bump is the set of struct pairs where a field is silently left empty today. The repository's own test files are organised around exactly those areas: case-insensitive matching, different types, field name mapping and tags.
Editorial conclusion
Adopt jinzhu/copier if you need name-based mapping between a small number of Go structs, slices or maps and you accept that mismatches are mostly silent. Do not adopt it if you need compile-time field mapping, automatic deep-clone semantics for shared pointers, or a mapping layer that fails loudly on every unexpected field. Before wiring it into a service, verify three things yourself: what happens when a required field is missing (the must tag is documented to panic or error), whether nested pointer fields are shared or cloned, and whether your go.mod toolchain is compatible with the module's go 1.13 directive.
Frequently asked questions
How do I install jinzhu/copier?
Install Go first, then run go get -u github.com/jinzhu/copier. The package is a library, so after that you import github.com/jinzhu/copier in your Go code.
How do I use jinzhu/copier to copy one struct into another?
Define the source and destination types, then call copier.Copy with pointers to both, as in copier.Copy(&employee, &user). Copier matches members by name, and the README's basic example produces Employee{Name:"Jinzhu", Age:18, DoubleAge:36, SuperRole:"Super Admin"}.
What does the copier:"must" tag do in jinzhu/copier?
It forces a field to be copied and results in a panic or an error if the field cannot be copied. The copier:"must,nopanic" variant returns an error instead of panicking.
Does jinzhu/copier copy between slices and maps?
Yes. The README documents slice to slice, struct to slice, and map to map copying, including a map[int]int copied into a map[int32]int8 with the element types converted.
What licence does jinzhu/copier use?
The repository is MIT licensed. The go.mod file declares only the module path and a go 1.13 directive, so the package has no runtime dependencies of its own.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/jinzhu-copier)Star history chart for your README
[](https://hysenlabs.com/projects/jinzhu-copier)Was this useful?