go语言复制一个struct 对象

 

go语言复制一个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
  log.Println("b:", b)
  log.Println("a:", a)
}

  其实,struct 对象直接赋值给其它变量,就可以了。赋值时,为值拷贝。

go语言复制一个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)
}

  

上一篇:全局路由首位配置


下一篇:3. meta标签