gitee.com/quant1x/gox@v1.21.2/api/copier_readme.md (about) 1 # Copier 2 3 I am a copier, I copy everything from one to another 4 5 [![test status](https://github.com/jinzhu/copier/workflows/tests/badge.svg?branch=master "test status")](https://github.com/jinzhu/copier/actions) 6 7 ## Features 8 9 * Copy from field to field with same name 10 * Copy from method to field with same name 11 * Copy from field to method with same name 12 * Copy from slice to slice 13 * Copy from struct to slice 14 * Copy from map to map 15 * Enforce copying a field with a tag 16 * Ignore a field with a tag 17 * Deep Copy 18 19 ## Usage 20 21 ```go 22 package main 23 24 import ( 25 "fmt" 26 "github.com/jinzhu/copier" 27 ) 28 29 type User struct { 30 Name string 31 Role string 32 Age int32 33 EmployeCode int64 `copier:"EmployeNum"` // specify field name 34 35 // Explicitly ignored in the destination struct. 36 Salary int 37 } 38 39 func (user *User) DoubleAge() int32 { 40 return 2 * user.Age 41 } 42 43 // Tags in the destination Struct provide instructions to copier.Copy to ignore 44 // or enforce copying and to panic or return an error if a field was not copied. 45 type Employee struct { 46 // Tell copier.Copy to panic if this field is not copied. 47 Name string `copier:"must"` 48 49 // Tell copier.Copy to return an error if this field is not copied. 50 Age int32 `copier:"must,nopanic"` 51 52 // Tell copier.Copy to explicitly ignore copying this field. 53 Salary int `copier:"-"` 54 55 DoubleAge int32 56 EmployeId int64 `copier:"EmployeNum"` // specify field name 57 SuperRole string 58 } 59 60 func (employee *Employee) Role(role string) { 61 employee.SuperRole = "Super " + role 62 } 63 64 func main() { 65 var ( 66 user = User{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 200000} 67 users = []User{{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 100000}, {Name: "jinzhu 2", Age: 30, Role: "Dev", Salary: 60000}} 68 employee = Employee{Salary: 150000} 69 employees = []Employee{} 70 ) 71 72 copier.Copy(&employee, &user) 73 74 fmt.Printf("%#v \n", employee) 75 // Employee{ 76 // Name: "Jinzhu", // Copy from field 77 // Age: 18, // Copy from field 78 // Salary:150000, // Copying explicitly ignored 79 // DoubleAge: 36, // Copy from method 80 // EmployeeId: 0, // Ignored 81 // SuperRole: "Super Admin", // Copy to method 82 // } 83 84 // Copy struct to slice 85 copier.Copy(&employees, &user) 86 87 fmt.Printf("%#v \n", employees) 88 // []Employee{ 89 // {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeId: 0, SuperRole: "Super Admin"} 90 // } 91 92 // Copy slice to slice 93 employees = []Employee{} 94 copier.Copy(&employees, &users) 95 96 fmt.Printf("%#v \n", employees) 97 // []Employee{ 98 // {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeId: 0, SuperRole: "Super Admin"}, 99 // {Name: "jinzhu 2", Age: 30, Salary:0, DoubleAge: 60, EmployeId: 0, SuperRole: "Super Dev"}, 100 // } 101 102 // Copy map to map 103 map1 := map[int]int{3: 6, 4: 8} 104 map2 := map[int32]int8{} 105 copier.Copy(&map2, map1) 106 107 fmt.Printf("%#v \n", map2) 108 // map[int32]int8{3:6, 4:8} 109 } 110 ``` 111 112 ### Copy with Option 113 114 ```go 115 copier.CopyWithOption(&to, &from, copier.Option{IgnoreEmpty: true, DeepCopy: true}) 116 ``` 117 118 ## Contributing 119 120 You can help to make the project better, check out [http://gorm.io/contribute.html](http://gorm.io/contribute.html) for 121 things you can do. 122 123 # Author 124 125 **jinzhu** 126 127 * <http://github.com/jinzhu> 128 * <wosmvp@gmail.com> 129 * <http://twitter.com/zhangjinzhu> 130 131 ## License 132 133 Released under the [MIT License](https://github.com/jinzhu/copier/blob/master/License).