golang 关于引用和指针的深拷贝的问题

问题现象

定义一个对象,对象中嵌套定义了map类型和一个对象,复制原来的对象生成新的对象,改变新对象中map的值和嵌套的对象的值会导致原来的对象的值也被改变。

现象模拟

package main

import (
	"encoding/json"
	"fmt"
)

type StudyCopy struct {
	Name      string
	Reference map[string]interface{}
	Nested    *Nested
}
type Nested struct {
	Name string
}

func NewCopy(parent *StudyCopy) *StudyCopy {
	//copy
	new := *parent
	return &new
}

func NewOldCopy() *StudyCopy {
	return &StudyCopy{
		Name: "oldCopy",
		Reference: map[string]interface{}{
			"test": "testOld",
		},
		Nested: &Nested{
			Name: "oldNested",
		},
	}
}

func ExceNewCopy(copy *StudyCopy, sourceReference map[string]interface{}) *StudyCopy {
	if copy.Reference == nil {
		copy.Reference = make(map[string]interface{})
	}
	if copy.Nested == nil {
		copy.Nested = &Nested{}
	}
	for k, v := range sourceReference {
		copy.Reference[k] = v
	}
	copy.Nested.Name = "newNested"
	return copy
}

func main() {
	//旧对象初始化
	oldCopy := NewOldCopy()
	//新对象初始化
	newCopy := NewCopy(oldCopy)
	sourceReference := map[string]interface{}{
		"test": "testNew",
	}
	newCopy = ExceNewCopy(newCopy, sourceReference)
	jsonO, _ := json.Marshal(oldCopy)
	jsonN, _ := json.Marshal(newCopy)
	fmt.Println("old:", string(jsonO), "new:", string(jsonN))
}

 结果

 解决方案

 涉及到引用和指针的拷贝问题,需要深度拷贝,即new一个新的对象or引用,做值赋值。

代码实现 

 

package main

import (
	"encoding/json"
	"fmt"
)

type StudyCopy struct {
	Name      string
	Reference map[string]interface{}
	Nested    *Nested
}
type Nested struct {
	Name string
}

func NewCopy(parent *StudyCopy) *StudyCopy {
	//copy
	new := *parent
	//引用类型,深度拷贝
	new.Reference = DeepCopy(parent.Reference).(map[string]interface{})
    //指针类型,深度拷贝
	new.Nested = &Nested{
		Name: parent.Name,
	}
	return &new
}

func NewOldCopy() *StudyCopy {
	return &StudyCopy{
		Name: "oldCopy",
		Reference: map[string]interface{}{
			"test": "testOld",
		},
		Nested: &Nested{
			Name: "oldNested",
		},
	}
}

func ExceNewCopy(copy *StudyCopy, sourceReference map[string]interface{}) *StudyCopy {
	if copy.Reference == nil {
		copy.Reference = make(map[string]interface{})
	}
	if copy.Nested == nil {
		copy.Nested = &Nested{}
	}
	for k, v := range sourceReference {
		copy.Reference[k] = v
	}
	copy.Nested.Name = "newNested"
	return copy
}

func DeepCopy(value interface{}) interface{} {
	valueMap, ok := value.(map[string]interface{})
	if !ok {
		return value
	}
	newMap := make(map[string]interface{})
	for k, v := range valueMap {
		newMap[k] = DeepCopy(v)
	}
	return newMap
}

func main() {
	//旧对象初始化
	oldCopy := NewOldCopy()
	//新对象初始化
	newCopy := NewCopy(oldCopy)
	sourceReference := map[string]interface{}{
		"test": "testNew",
	}
	newCopy = ExceNewCopy(newCopy, sourceReference)
	jsonO, _ := json.Marshal(oldCopy)
	jsonN, _ := json.Marshal(newCopy)
	fmt.Println("old:", string(jsonO), "new:", string(jsonN))

结果 

总结

 涉及到指针和引用的拷贝问题,需要多加关注,不做深度拷贝,还有可能会引发并发问题 。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值