go 不同结构复制
A struct variable in Go can be copied to another variable very easily using the assignment statement:
使用赋值语句,可以很容易地将Go中的struct变量复制到另一个变量:
package main
import (
"fmt"
)
type Dog struct {
age int
name string
}
func main() {
roger := Dog{5, "Roger"}
mydog := roger
if roger == mydog {
fmt.Println("Roger and mydog are equal structs")
}
}
mydog
and roger
are two separate variables, but comparing them property-by-property they are exactly the same.
mydog
和roger
是两个单独的变量,但是逐个属性地比较它们是完全相同的。
We can now modify every property of those variables without affecting the other object, because the Dog
struct contains basic types, which are copied by value.
现在,我们可以修改那些变量的每个属性而不会影响其他对象,因为Dog
结构包含基本类型,这些基本类型按值复制。
If we had a reference type in our struct, this was not that easy, because the pointer
would be copied, not the value, so it would have been a shallow copy.
如果我们在结构中有一个引用类型,那么就不那么容易了,因为pointer
将被复制,而不是值,因此将被复制为浅表 。
For example
例如
type Cat struct {
age int
name string
friends []string
}
The list of friends of our cat is a slice, a reference type (along with pointers, maps, functions and channels), so if you had a cat object and you copy it like we did for the Dog example above, you couldn’t modify the friends property without affecting the other object as well.
我们的猫的朋友列表是一个切片,一个引用类型(以及指针,地图,函数和通道),因此,如果您有一个猫对象,并且像上面的Dog示例一样进行复制,则无法修改friends属性,而不影响其他对象。
A deep copy is needed.
需要深拷贝 。
深度复制结构 (Deep copy a struct)
You can perform such deep copy manually. In the case of a slice, you can perform the copy like this:
您可以手动执行这种深层复制。 对于切片,您可以执行以下复制:
package main
import (
"fmt"
)
type Cat struct {
age int
name string
friends []string
}
func main() {
wilson := Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}
nikita := wilson
nikita.friends = make([]string, len(wilson.friends))
copy(nikita.friends, wilson.friends)
nikita.friends = append(nikita.friends, "Syd")
fmt.Println(wilson)
fmt.Println(nikita)
}
The above code will result in wilson being {7 Wilson [Tom Tabata Willie]}
and nikita being {7 Wilson [Tom Tabata Willie Syd]}
上面的代码将导致威尔逊是{7 Wilson [Tom Tabata Willie]}
和尼基塔感{7 Wilson [Tom Tabata Willie Syd]}
So you can see the slice is a completely different object, not just a reference to the same slice.
因此,您可以看到切片是一个完全不同的对象,而不仅仅是对同一切片的引用。
使用深层复制库 (Using a deep copy library)
https://github.com/jinzhu/copier is (one of the many) deep copy libs/helpers out there. It makes it really easy to deep copy a struct without explicitly managing each field. Here’s an example, all we need is copier.Copy(&nikita, &wilson)
and we’re set:
https://github.com/jinzhu/copier是(其中众多)深层复制libs / helpers。 无需显式管理每个字段,就很容易对结构进行深层复制。 这是一个示例,我们需要的只是copier.Copy(&nikita, &wilson)
并设置为:
package main
import (
"fmt"
"github.com/jinzhu/copier"
)
type Cat struct {
age int
name string
friends []string
}
func main() {
wilson := Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}
nikita := Cat{}
copier.Copy(&nikita, &wilson)
nikita.friends = append(nikita.friends, "Syd")
fmt.Println(wilson)
fmt.Println(nikita)
}
go 不同结构复制