We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bc27893 commit f9e8bccCopy full SHA for f9e8bcc
json/json.go
@@ -0,0 +1,27 @@
1
+package json
2
+
3
+import (
4
+ "encoding/json"
5
+ "reflect"
6
+)
7
8
+// Copy makes a new object which has same field key and value from 'src'
9
+func Copy(dst interface{}, src interface{}) (err error) {
10
11
+ if dst == nil || (reflect.ValueOf(dst).Kind() == reflect.Ptr && reflect.ValueOf(dst).IsNil()) {
12
+ dst = reflect.New(reflect.TypeOf(dst))
13
+ }
14
+ if src == nil || (reflect.ValueOf(src).Kind() == reflect.Ptr && reflect.ValueOf(src).IsNil()) {
15
+ src = reflect.New(reflect.TypeOf(src))
16
17
18
+ bytes, err := json.Marshal(src)
19
+ if err != nil {
20
+ return
21
22
+ err = json.Unmarshal(bytes, dst)
23
24
25
26
+ return nil
27
+}
0 commit comments