如何深度复制对象

This question already has an answer here:

I have a complex data structure, which defines a type P, and I'd like to perform a deep copy of an instance of such a data structure. I have found this library but, considering the semantics of the Go language, wouldn't a method like the following be more idiomatic?:

func (receiver P) copy() *P{
   return &receiver
}

Since the method receives a value of type P (and values are always passed by copy), the result should be a reference to a deep copy of the source, like in this example:

src := new(P)
dcp := src.copy()

Indeed,

src != dst => true
reflect.DeepEqual(*src, *dst) => true
</div>

this test shows that your method doesn't do a copy

package main

import (
    "fmt"
)

type teapot struct {
   t []string
}
type P struct {
   a string
   b teapot
}

func (receiver P) copy() *P{
   return &receiver
}

func main() {

x:=new(P)
x.b.t=[]string{"aa","bb"}
y:=x.copy()

y.b.t[1]="cc"  // y is altered but x should be the same

fmt.Println(x)  // but as you can see...

}

https://play.golang.org/p/xL-E4XKNXYe