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

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -