go语言复制一个struct 对象
2021-06-05 21:02
标签:div main bsp 技术 一个 str ack UNC port ----------------- 其实,struct 对象直接赋值给其它变量,就可以了。赋值时,为值拷贝。 go语言复制一个struct 对象 标签:div main bsp 技术 一个 str ack UNC port 原文地址:https://www.cnblogs.com/oxspirt/p/14623131.htmlpackage 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)
}
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)
}
上一篇:linux多进/线程编程(2)—— fork函数和进程间“共享”数据
下一篇:javax.persistence.spi.PersistenceUnitInfo.getSharedCacheMode()Ljavax/persistence/SharedCacheMode;