go 不同结构复制_在Go中复制结构

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")
	}
}

play

mydog and roger are two separate variables, but comparing them property-by-property they are exactly the same.

mydogroger是两个单独的变量,但是逐个属性地比较它们是完全相同的。

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)
}

play

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)
}

翻译自: https://flaviocopes.com/go-copying-structs/

go 不同结构复制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值