[Awesome Go] Use copy to copy structs to/from others structs easier

Copy

This package is meant to make copying of structs to/from others structs a bit easier.

Nested structures, embedded types, pointers, sql null types are supported.

Installation

Standard go get:

1
$ go get -u github.com/gotidy/copy

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
type Person struct {
Name string
MiddleName *string
Surname string
}

type User struct {
Person
Email string
Age int8
Married bool
}

type Employee struct {
Name string
MiddleName string
Surname string
Email string
Age int
}

src := User{
Person: Person{
Name: "John",
MiddleName: nil,
Surname: "Smith",
},
Email: "[email protected]",
Age: 33,
Married: false,
}
dst := Employee{}

copiers := copy.New() // New("json")
copiers.Copy(&dst, &src)

// Or more fast use case is to create the type specific copier.

copier := copiers.Get(&Employee{}, &User{}) // Created once for a pair of types.
copier.Copy(&dst, &src)

References

[1] GitHub - gotidy/copy: Package for fast copying structs of different types - https://github.com/gotidy/copy

[2] copy · pkg.go.dev - https://pkg.go.dev/github.com/gotidy/copy

[3] GitHub - ulule/deepcopier: simple struct copying for golang - https://github.com/ulule/deepcopier

[4] GitHub - jinzhu/copier: Copier for golang, copy value from struct to struct and more - https://github.com/jinzhu/copier