go - Deepcopying struct having pointer-to 0 value in golang -
i have struct in golang
below
type test struct { prop *int }
i want take deepcopy of struct object when prop
pointer-to 0 value. real struct has lot more fields in , want deepcopy of entire struct obj. tried use gob
encode-decode way converts pointer-to 0 nil pointer due consequence of design mentioned here. tried use reflect.copy
panics error panic: reflect: call of reflect.copy on struct value
. there better way deepcopy such struct objects?
edit: tried use json
encoding/decoding , kind of worked. don't know drawbacks.
func deepcopy(a, b interface{}) { byt, _ := json.marshal(a) json.unmarshal(byt, b) }
any comments on solution?
https://play.golang.org/p/fvkw62bydm
i used https://github.com/mohae/deepcopy/blob/master/deepcopy.go example. reflect.copy works slices or arrays. can see, using reflection right way, more complex calling reflect.copy. there few other packages, implement deep copy, don't have experience of packages.
wiki
Comments
Post a Comment