-----------------
package main import ( "log" ) type meta struct { name string age int } func (m *meta) copy(dest *meta) { *dest = *m } func main() { a := meta{name:"aaaaaaa", age:123} log.Println(a) var b meta a.copy(&b) b.name="fly" b.age = 77 log.Println("b:", b) log.Println("a:", a) }
其实,struct 对象直接赋值给其它变量,就可以了。赋值时,为值拷贝。
package main import ( "log" ) type meta struct { name string age int } func (m *meta) copy(dest *meta) { *dest = *m } func main() { a := meta{name:"aaaaaaa", age:123} log.Println(a) var b meta a.copy(&b) b.name="fly" b.age = 77 c := a c.name = "zzz" c.age = 9 log.Println("b:", b) log.Println("a:", a) log.Println("c:", c) }