Application scenarios:
The copier library allows the use of dto in go, which is equivalent to .net automapper
General usage: For more information, please go to github
jinzhu/copier: Copier for golang, copy value from struct to struct and more (github.com)
package main import ( "fmt" "github.com/jinzhu/copier" ) typeUser struct { Name string Role string Age int32 EmployeeCode int64 `copier:"EmployeeNum"` // specify field name // Explicitly ignored in the destination struct. Salary int } func (user *User) DoubleAge() int32 { return 2 * user.Age } // Tags in the destination Struct provide instructions to copier.Copy to ignore // or enforce copying and to panic or return an error if a field was not copied. typeEmployee struct { // Tell copier.Copy to panic if this field is not copied. Name string `copier:"must"` // Tell copier.Copy to return an error if this field is not copied. Age int32 `copier:"must,nopanic"` // Tell copier.Copy to explicitly ignore copying this field. Salary int `copier:"-"` DoubleAge int32 EmployeeId int64 `copier:"EmployeeNum"` // specify field name SuperRole string } func (employee *Employee) Role(role string) { employee.SuperRole = "Super " + role } func main() { var ( user = User{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 200000} users = []User{<!-- -->{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 100000}, {Name: "jinzhu 2", Age : 30, Role: "Dev", Salary: 60000}} employee = Employee{Salary: 150000} employees = []Employee{} ) copier.Copy( & amp;employee, & amp;user) fmt.Printf("%#v \\ ", employee) //Employee{ // Name: "Jinzhu", // Copy from field // Age: 18, // Copy from field // Salary:150000, // Copying explicitly ignored // DoubleAge: 36, // Copy from method // EmployeeId: 0, // Ignored // SuperRole: "Super Admin", // Copy to method // } //Copy struct to slice copier.Copy( & amp;employees, & amp;user) fmt.Printf("%#v \\ ", employees) //[]Employee{ // {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeeId: 0, SuperRole: "Super Admin"} // } //Copy slice to slice employees = []Employee{} copier.Copy( & amp;employees, & amp;users) fmt.Printf("%#v \\ ", employees) //[]Employee{ // {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeeId: 0, SuperRole: "Super Admin"}, // {Name: "jinzhu 2", Age: 30, Salary:0, DoubleAge: 60, EmployeeId: 0, SuperRole: "Super Dev"}, // } //Copy map to map map1 := map[int]int{3: 6, 4: 8} map2 := map[int32]int8{} copier.Copy( & amp;map2, map1) fmt.Printf("%#v \\ ", map2) // map[int32]int8{3:6, 4:8} }
Changes in painting style:
I want to convert the user role list in the user information into []int32 slices and return them to the front end. The above method does not work at first. I cannot directly convert []UserRole into []int32. Fortunately, The workers were not discouraged or compromised, and finally solved the problem through constant testing. . .
The code is as follows
//User information struct type Users struct { Account string `json:"account" gorm:"unique;size:50;comment:account"` Password string `json:"-" gorm:"comment:password" copier:"-"` Slat string `json:"-" gorm:"comment:encryption salt" copier:"-"` Name string `json:"name" gorm:"size:50;comment:name"` Age uint8 `json:"age,omitempty" gorm:"size:4;comment:age"` Gender uint8 `json:"gender,omitempty" gorm:"size:1;comment:gender"` Phone string `json:"phone,omitempty" gorm:"size:11;comment:phone"` Email string `json:"email,omitempty" gorm:"size:100;comment:email"` IsActive bool `json:"isActive,omitempty" gorm:"size:1;comment: Whether to activate"` UserRoles []UserRole `json:"roles,omitempty" gorm:"foreignKey:UserID"` Entity } // The dto that needs to be mapped typeUserDto struct { Account string `json:"account"` Name string `json:"name"` Age uint8 `json:"age,omitempty"` Gender uint8 `json:"gender,omitempty"` Phone string `json:"phone,omitempty"` Email string `json:"email,omitempty"` IsActive bool `json:"isActive"` IsDelete bool `json:"isDelete"` CreateBy string `json:"createBy,omitempty"` CreateTime time.Time `json:"createTime,omitempty"` UserRoles []int32 `json:"roles,omitempty"` }
Use copier's
CopyWithOption method to customize mapping rules
// Customized TypeConverter, Users converted to Dto var UserToDto = []copier.TypeConverter{ { // source type SrcType: []domain.UserRole{}, // mapping type DstType: []int32{}, // Conversion function Fn: func(src interface{}) (interface{}, error) { //Type assertion, convert src to domain.UserRole s, ok := src.([]domain.UserRole) if !ok { return nil, errors.New("src type not matching") } //Create a slice of RoleID r := make([]int32, 0) for _, v := range s { r = append(r, v.RoleID) } return r, nil }, }, { // Dto []int32 to []domain.Users SrcType: []int32{}, DstType: []domain.UserRole{}, Fn: func(src interface{}) (interface{}, error) { s, ok := src.([]int32) if !ok { return nil, errors.New("src type not matching") } r := make([]domain.UserRole, 0) for _, v := range s { r = append(r, domain.UserRole{RoleID: v}) } return r, nil }, }, } // When converting, just write the following code, it is so simple and smooth err = copier.CopyWithOption( & amp;udto, user, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: contracts.UserToDto})
Final Results:
Welcome to give advice and discuss. .
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Go Skill Tree Home Page Overview 4247 people are learning the system