Go语言深度拷贝工具deepcopy的使用教程

 更新时间:2022年09月15日 11:25:43   作者:Go学堂  
今天给大家推荐的工具是deepcopy,一个可以对指针、接口、切片、结构体、Map都能进行深拷贝的工具,感兴趣的小伙伴快跟随小编一起学习学习

今天给大家推荐的工具是deepcopy,一个可以对指针、接口、切片、结构体、Map都能进行深拷贝的工具。在Go中需要对一个变量进行拷贝时分浅拷贝和深拷贝。浅拷贝就是拷贝后就是无论改变新值还是原值都对对另一个产生影响,比如切片。而深拷贝则是将目标值完全拷贝一份,消除这种影响。

实现原理分析

深拷贝的实现原理本质上是通过反射实现。通过将源对象转换成接口,再对接口通过反射判断其类型,进而进行深度拷贝。如下就是该包的完全实现:

package deepcopy

import (
	"reflect"
	"time"
)

// Interface for delegating copy process to type
type Interface interface {
	DeepCopy() interface{}
}

// Iface is an alias to Copy; this exists for backwards compatibility reasons.
func Iface(iface interface{}) interface{} {
	return Copy(iface)
}

// Copy creates a deep copy of whatever is passed to it and returns the copy
// in an interface{}.  The returned value will need to be asserted to the
// correct type.
func Copy(src interface{}) interface{} {
	if src == nil {
		return nil
	}

	// Make the interface a reflect.Value
	original := reflect.ValueOf(src)

	// Make a copy of the same type as the original.
	cpy := reflect.New(original.Type()).Elem()

	// Recursively copy the original.
	copyRecursive(original, cpy)

	// Return the copy as an interface.
	return cpy.Interface()
}

// copyRecursive does the actual copying of the interface. It currently has
// limited support for what it can handle. Add as needed.
func copyRecursive(original, cpy reflect.Value) {
	// check for implement deepcopy.Interface
	if original.CanInterface() {
		if copier, ok := original.Interface().(Interface); ok {
			cpy.Set(reflect.ValueOf(copier.DeepCopy()))
			return
		}
	}

	// handle according to original's Kind
	switch original.Kind() {
	case reflect.Ptr:
		// Get the actual value being pointed to.
		originalValue := original.Elem()

		// if  it isn't valid, return.
		if !originalValue.IsValid() {
			return
		}
		cpy.Set(reflect.New(originalValue.Type()))
		copyRecursive(originalValue, cpy.Elem())

	case reflect.Interface:
		// If this is a nil, don't do anything
		if original.IsNil() {
			return
		}
		// Get the value for the interface, not the pointer.
		originalValue := original.Elem()

		// Get the value by calling Elem().
		copyValue := reflect.New(originalValue.Type()).Elem()
		copyRecursive(originalValue, copyValue)
		cpy.Set(copyValue)

	case reflect.Struct:
		t, ok := original.Interface().(time.Time)
		if ok {
			cpy.Set(reflect.ValueOf(t))
			return
		}
		// Go through each field of the struct and copy it.
		for i := 0; i < original.NumField(); i++ {
			// The Type's StructField for a given field is checked to see if StructField.PkgPath
			// is set to determine if the field is exported or not because CanSet() returns false
			// for settable fields.  I'm not sure why.  -mohae
			if original.Type().Field(i).PkgPath != "" {
				continue
			}
			copyRecursive(original.Field(i), cpy.Field(i))
		}

	case reflect.Slice:
		if original.IsNil() {
			return
		}
		// Make a new slice and copy each element.
		cpy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))
		for i := 0; i < original.Len(); i++ {
			copyRecursive(original.Index(i), cpy.Index(i))
		}

	case reflect.Map:
		if original.IsNil() {
			return
		}
		cpy.Set(reflect.MakeMap(original.Type()))
		for _, key := range original.MapKeys() {
			originalValue := original.MapIndex(key)
			copyValue := reflect.New(originalValue.Type()).Elem()
			copyRecursive(originalValue, copyValue)
			copyKey := Copy(key.Interface())
			cpy.SetMapIndex(reflect.ValueOf(copyKey), copyValue)
		}

	default:
		cpy.Set(original)
	}
}

基本使用

拷贝切片

	a := []int{1,2,3}
	dst := deepcopy.Copy(a)
	a1 := dst.([]int)
	a1[0] = 2
	fmt.Println(a, a1) //a:[1 2 3] a1:[2 2 3]

拷贝map

	a := make(map[string]int)
	a["k1"] = 1
	a["k2"] = 2
	a["k3"] = 3
	dst := deepcopy.Copy(a)
	a1 := dst.(map[string]int)
	a1["k1"] = 10
	fmt.Println(a, a1) //a:map[k1:1 k2:2 k3:3] a1:map[k1:10 k2:2 k3:3]

更多项目详情请查看如下链接。

开源项目地址:https://github.com/mohae/deepcopy

到此这篇关于Go语言深度拷贝工具deepcopy的使用教程的文章就介绍到这了,更多相关Go深度拷贝deepcopy内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用go读取gzip格式的压缩包的操作

    使用go读取gzip格式的压缩包的操作

    这篇文章主要介绍了使用go读取gzip格式的压缩包的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • Go 常量基础概念(声明更改只读)

    Go 常量基础概念(声明更改只读)

    这篇文章主要为大家介绍了Go常量基础概念包括常量的声明更改只读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Go常用技能日志log包创建使用示例

    Go常用技能日志log包创建使用示例

    这篇文章主要为大家介绍了Go常用技能日志log包创建使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Go语言面向对象中的多态你学会了吗

    Go语言面向对象中的多态你学会了吗

    面向对象中的多态(Polymorphism)是指一个对象可以具有多种不同的形态或表现方式,本文将通过一些简单的示例为大家讲解一下多态的实现,需要的可以参考下
    2023-07-07
  • golang逗号ok模式整合demo

    golang逗号ok模式整合demo

    这篇文章主要为大家介绍了golang逗号ok模式整合demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • Go语言中的UTF-8实现

    Go语言中的UTF-8实现

    这篇文章主要介绍了Go语言中的UTF-8实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Golang实现定时任务的几种方法小结

    Golang实现定时任务的几种方法小结

    在 Golang 开发中,定时任务是常见的需求,本文将介绍几种在 Golang 中实现定时任务的方法,包括 time 包的定时器、ticker,以及第三方库 cron,并通过示例代码展示它们的使用方式,需要的朋友可以参考下
    2024-01-01
  • Golang之sync.Pool使用详解

    Golang之sync.Pool使用详解

    这篇文章主要介绍了Golang之sync.Pool使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • go RWMutex的实现示例

    go RWMutex的实现示例

    本文主要来介绍读写锁的一种Go语言的实现方式RWMutex,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Golang实现协程超时控制的方式总结

    Golang实现协程超时控制的方式总结

    我们知道,go协程如果不做好处理,很容易造成内存泄漏,所以对goroutine做超时控制,才能有效避免这种情况发生,本文为大家整理了两个常见的Golang超时控制方法,需要的可以收藏一下
    2023-05-05

最新评论