Go语言如何深度拷贝对象

hittata ·
#### 当结构体中有小写成员变量时,上述方式无效 ``` package main import ( "bytes" "encoding/gob" "log" ) func deepCopy(dst, src interface{}) error { var buf bytes.Buffer if err := gob.NewEncoder(&buf).Encode(src); err != nil { return err } return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst) } type User struct { name string // 小写变量,不能被deepCopy函数拷贝成功 Age int } func main() { src := &User{name: "src", Age: 100} dst := new(User) if err := deepCopy(dst, src); err != nil { log.Fatal(err) } log.Printf("%+v", b) } ``` ![image.png](https://static.studygolang.com/190211/8db741de76a7b6bafd19a688ae700b3e.png)
#1