在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
----------------- 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) }
|
请发表评论