Skip to content
forked from jinzhu/copier

Copier for golang, copy value from struct to struct and more

License

Notifications You must be signed in to change notification settings

kyleboyle/copier

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Copier

I am a copier, I copy everything from a struct to another struct

Features

  • Copy field to field if name exactly match
  • Copy from method to field if method name and field name exactly match
  • Copy from field to method if field name and method name exactly match
  • Copy slice to slice
  • Copy struct to slice

Usage

import . "github.com/jinzhu/copier"

type User struct {
	Name string
	Role string
	Age  int32
}

func (user *User) DoubleAge() int32 {
	return 2 * user.Age
}

type Employee struct {
	Name      string
	Age       int32
	DoubleAge int32
	EmployeId int64
	SuperRule string
}

func (employee *Employee) Role(role string) {
	employee.SuperRule = "Super " + role
}

user := User{Name: "Jinzhu", Age: 18, Role: "Admin"}
employee := Employee{}

Copy(&employee, &user)

// employee => Employee{ Name: "Jinzhu",           // Copy from field
//                       Age: 18,                  // Copy from field
//                       DoubleAge: 36,            // Copy from method
//                       EmployeeId: 0,            // Just ignored
//                       SuperRule: "Super Admin", // Copy to method
//                      }

// Copy struct to slice
user := User{Name: "hello", Age: 18, Role: "User"}
employees := []Employee{}
Copy(&employees, &user)
// employees => [{hello 18 0 36 Super User}]


// Copy slice to slice
users := []User{{Name: "Jinzhu", Age: 18, Role: "Admin"}, {Name: "jinzhu 2", Age: 30, Role: "Dev"}}
employees := []Employee{}
Copy(&employees, &users)

// employees => [{hello 18 0 36 Super User} {Jinzhu 18 0 36 Super Admin} {jinzhu 2 30 0 60 Super Dev}]

Author

jinzhu

License

Released under the MIT License.

About

Copier for golang, copy value from struct to struct and more

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.6%
  • Ruby 0.4%