最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Go語(yǔ)言深度拷貝工具deepcopy的使用教程

 更新時(shí)間:2022年09月15日 11:25:43   作者:Go學(xué)堂  
今天給大家推薦的工具是deepcopy,一個(gè)可以對(duì)指針、接口、切片、結(jié)構(gòu)體、Map都能進(jìn)行深拷貝的工具,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)學(xué)習(xí)

今天給大家推薦的工具是deepcopy,一個(gè)可以對(duì)指針、接口、切片、結(jié)構(gòu)體、Map都能進(jìn)行深拷貝的工具。在Go中需要對(duì)一個(gè)變量進(jìn)行拷貝時(shí)分淺拷貝和深拷貝。淺拷貝就是拷貝后就是無(wú)論改變新值還是原值都對(duì)對(duì)另一個(gè)產(chǎn)生影響,比如切片。而深拷貝則是將目標(biāo)值完全拷貝一份,消除這種影響。

實(shí)現(xiàn)原理分析

深拷貝的實(shí)現(xiàn)原理本質(zhì)上是通過(guò)反射實(shí)現(xiàn)。通過(guò)將源對(duì)象轉(zhuǎn)換成接口,再對(duì)接口通過(guò)反射判斷其類(lèi)型,進(jìn)而進(jìn)行深度拷貝。如下就是該包的完全實(shí)現(xiàn):

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]

更多項(xiàng)目詳情請(qǐng)查看如下鏈接。

開(kāi)源項(xiàng)目地址:https://github.com/mohae/deepcopy

到此這篇關(guān)于Go語(yǔ)言深度拷貝工具deepcopy的使用教程的文章就介紹到這了,更多相關(guān)Go深度拷貝deepcopy內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用go讀取gzip格式的壓縮包的操作

    使用go讀取gzip格式的壓縮包的操作

    這篇文章主要介紹了使用go讀取gzip格式的壓縮包的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Go 常量基礎(chǔ)概念(聲明更改只讀)

    Go 常量基礎(chǔ)概念(聲明更改只讀)

    這篇文章主要為大家介紹了Go常量基礎(chǔ)概念包括常量的聲明更改只讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Go常用技能日志log包創(chuàng)建使用示例

    Go常用技能日志log包創(chuàng)建使用示例

    這篇文章主要為大家介紹了Go常用技能日志log包創(chuàng)建使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Go語(yǔ)言面向?qū)ο笾械亩鄳B(tài)你學(xué)會(huì)了嗎

    Go語(yǔ)言面向?qū)ο笾械亩鄳B(tài)你學(xué)會(huì)了嗎

    面向?qū)ο笾械亩鄳B(tài)(Polymorphism)是指一個(gè)對(duì)象可以具有多種不同的形態(tài)或表現(xiàn)方式,本文將通過(guò)一些簡(jiǎn)單的示例為大家講解一下多態(tài)的實(shí)現(xiàn),需要的可以參考下
    2023-07-07
  • golang逗號(hào)ok模式整合demo

    golang逗號(hào)ok模式整合demo

    這篇文章主要為大家介紹了golang逗號(hào)ok模式整合demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Go語(yǔ)言中的UTF-8實(shí)現(xiàn)

    Go語(yǔ)言中的UTF-8實(shí)現(xiàn)

    這篇文章主要介紹了Go語(yǔ)言中的UTF-8實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Golang實(shí)現(xiàn)定時(shí)任務(wù)的幾種方法小結(jié)

    Golang實(shí)現(xiàn)定時(shí)任務(wù)的幾種方法小結(jié)

    在 Golang 開(kāi)發(fā)中,定時(shí)任務(wù)是常見(jiàn)的需求,本文將介紹幾種在 Golang 中實(shí)現(xiàn)定時(shí)任務(wù)的方法,包括 time 包的定時(shí)器、ticker,以及第三方庫(kù) cron,并通過(guò)示例代碼展示它們的使用方式,需要的朋友可以參考下
    2024-01-01
  • Golang之sync.Pool使用詳解

    Golang之sync.Pool使用詳解

    這篇文章主要介紹了Golang之sync.Pool使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • go RWMutex的實(shí)現(xiàn)示例

    go RWMutex的實(shí)現(xiàn)示例

    本文主要來(lái)介紹讀寫(xiě)鎖的一種Go語(yǔ)言的實(shí)現(xiàn)方式RWMutex,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Golang實(shí)現(xiàn)協(xié)程超時(shí)控制的方式總結(jié)

    Golang實(shí)現(xiàn)協(xié)程超時(shí)控制的方式總結(jié)

    我們知道,go協(xié)程如果不做好處理,很容易造成內(nèi)存泄漏,所以對(duì)goroutine做超時(shí)控制,才能有效避免這種情況發(fā)生,本文為大家整理了兩個(gè)常見(jiàn)的Golang超時(shí)控制方法,需要的可以收藏一下
    2023-05-05

最新評(píng)論

日土县| 永修县| 齐齐哈尔市| 泽普县| 黄平县| 镇康县| 阆中市| 巨野县| 长寿区| 永丰县| 灵寿县| 隆安县| 罗田县| 阳江市| 莱芜市| 柳州市| 玉屏| 图片| 郯城县| 榆林市| 盖州市| 金阳县| 宁河县| 平度市| 红原县| 都安| 宜君县| 资源县| 上栗县| 宁蒗| 大同县| 霍山县| 广宁县| 高尔夫| 梨树县| 汕头市| 东台市| 寿宁县| 当涂县| 理塘县| 宜章县|