https://github.com/maxtong1987/copycat
Recursively perform deep copy from source (src) to destination (dst) using reflection.
https://github.com/maxtong1987/copycat
deepcopy go golang reflection
Last synced: 5 months ago
JSON representation
Recursively perform deep copy from source (src) to destination (dst) using reflection.
- Host: GitHub
- URL: https://github.com/maxtong1987/copycat
- Owner: maxtong1987
- License: mit
- Created: 2020-09-12T01:03:45.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-06-29T15:42:50.000Z (almost 5 years ago)
- Last Synced: 2024-06-20T17:45:42.407Z (about 2 years ago)
- Topics: deepcopy, go, golang, reflection
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# copycat
[](https://pkg.go.dev/github.com/maxtong1987/copycat)
[](https://app.circleci.com/pipelines/github/maxtong1987/copycat?branch=master)
[](https://github.com/maxtong1987/copycat/actions)
[](https://coveralls.io/github/maxtong1987/copycat?branch=master)
[](https://goreportcard.com/report/github.com/maxtong1987/copycat)
Recursively perform deep copy from source (src) to destination (dst) using reflection until either end got exhausted. Support array, slice, struct, pointer and interface. Copying between different primitive types are tolerated.
## Getting started
To get the package, execute:
```sh
go get github.com/maxtong1987/copycat
```
To import the package, add the following line to your code:
```go
import "github.com/maxtong1987/copycat"
```
## Example
```go
package main
import (
"fmt"
"github.com/maxtong1987/copycat"
)
type subType struct {
X string
Y bool
Z int
}
type srcType struct {
A string
B int32
C float64
D []uint64
E subType
}
type destType struct {
A string
B int64
C float32
d []uint64
E *subType
}
func (d *destType) String() string {
return fmt.Sprintf("A:%s B:%v C:%v d:%v E:%+v", d.A, d.B, d.C, d.d, *d.E)
}
func main() {
src := srcType{
A: "a",
B: 123,
C: 0.0122,
D: []uint64{6, 7, 8, 9},
E: subType{X: "x", Y: true, Z: 100},
}
dst := &destType{}
copycat.DeepCopy(dst, src)
fmt.Print(dst)
}
```