当前位置:网站首页>Golang中的深拷贝与浅拷贝
Golang中的深拷贝与浅拷贝
2022-06-10 23:40:00 【JackMa_】
一、概念
1、深拷贝(Deep Copy)
拷贝的是数据本身,创造一个样的新对象,新创建的对象与原对象不共享内存,新创建的对象在内存中开辟一个新的内存地址,新对象值修改时不会影响原对象值。既然内存地址不同,释放内存地址时,可分别释放。
值类型的数据,默认全部都是深复制,Array、Int、String、Struct、Float,Bool。
2、浅拷贝(Shallow Copy)
拷贝的是数据地址,只复制指向的对象的指针,此时新对象和老对象指向的内存地址是一样的,新对象值修改时老对象也会变化。释放内存地址时,同时释放内存地址。
引用类型的数据,默认全部都是浅复制,Slice,Map。
二、本质区别
是否真正获取(复制)对象实体,而不是引用。
三、示例
浅拷贝
等号赋值
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
slice1 := []int{1,2,3,4,5}
slice2 := slice1
fmt.Println(slice1)
fmt.Println(slice2)
//同时改变两个数组
slice1[1]=100
fmt.Println(slice1)
fmt.Println(slice2)
fmt.Println("切片1指向的底层数组地址:",(*reflect.SliceHeader)(unsafe.Pointer(&slice1)))
fmt.Println("切片2指向的底层数组地址:",(*reflect.SliceHeader)(unsafe.Pointer(&slice2)))
}输出信息:
[1 2 3 4 5]
[1 2 3 4 5]
[1 100 3 4 5]
[1 100 3 4 5]
切片1指向的底层数组地址: &{824634425392 5 5}
切片2指向的底层数组地址: &{824634425392 5 5}关于copy函数:
1.copy只能用于切片,不能用于 map 等任何其他类型。
2.copy返回结果为一个 int 型值,表示 copy 从原切片src复制到目的切片的长度。
使用注意事项:
切片 dst 需要先初始化长度
在使用copy将 src 完全 复制 到 dst 时,需要初始化目的切片dst的长度。
1.如果 dst 长度小于 src 的长度,则 拷贝src中的部分内容;
2.如果大于,则全部拷贝过来,其余的空间填充该类型的默认值;
3.如果相等,刚好不多不少 copy 过来,所以,通常dst在初始化时即指定其为src的长度。
package main
import "fmt"
func main() {
src := []int{1, 2, 3, 5, 6, 7, 8}
fmt.Println("src len:", len(src), "src:", src)
dst := make([]int, 0)
copy(dst, src)
fmt.Println("dst len:", len(dst), "dst:", dst)
dst1 := make([]int, len(src)/2 )
copy(dst1, src)
fmt.Println("dst1 len:", len(dst1), "dst1:", dst1)
dst2 := make([]int, len(src))
copy(dst2, src)
fmt.Println("dst2 len:", len(dst2), "dst2:", dst2)
dst3 := make([]int, len(src) + 2)
copy(dst3, src)
fmt.Println("dst3 len:", len(dst3), "dst3:", dst3)
}
输出
src len: 7 src: [1 2 3 5 6 7 8]
dst len: 0 dst: []
dst1 len: 3 dst1: [1 2 3]
dst2 len: 7 dst2: [1 2 3 5 6 7 8]
dst3 len: 9 dst3: [1 2 3 5 6 7 8 0 0]
源切片中元素类型为引用类型时,拷贝的是引用
由于copy 函数,拷贝的是切片中的元素,所以如果切片元素的类型是引用类型,那么 copy 的也将是个引用。
如下面例子,matA 和 matB 地址不一样,但 matA[0] 和 matB[0] 的地址是一样的。
func wrongCopyMatrix() {
matA := [][]int{
{0, 1, 1, 0},
{0, 1, 1, 1},
{1, 1, 1, 0},
}
matB := make([][]int, len(matA))
copy(matB, matA)
fmt.Printf("%p, %p\n", matA, matA[0]) // 0xc0000c0000, 0xc0000c2000
fmt.Printf("%p, %p\n", matB, matB[0]) // 0xc0000c0050, 0xc0000c2000
}
如果想 copy 多维切片中的每一个切片类型的元素,那么就需要将每个切片元素进行 初始化 并 拷贝。注意是两步:
1.先初始化每维切片,
2.再拷贝。正确拷贝一个多维数组的打开方式:
func rightCopyMatrix() {
matA := [][]int{
{0, 1, 1, 0},
{0, 1, 1, 1},
{1, 1, 1, 0},
}
matB := make([][]int, len(matA))
for i := range matA {
matB[i] = make([]int, len(matA[i])) // 注意初始化长度
copy(matB[i], matA[i])
}
fmt.Printf("%p, %p\n", matA, matA[0]) // 0xc00005c050, 0xc000018560
fmt.Printf("%p, %p\n", matB, matB[0]) // 0xc00005c0a0, 0xc0000185c0
}
切片使用copy和等号复制的区别
1.性能方面:copy复制会比等号复制慢。 2.复制方式:copy复制为值复制,改变原切片的值不会影响新切片。而等号复制为指针复制,改变原切片或新切片都会对另一个产生影响。
深拷贝
(浅)拷贝对于值类型的话是完全拷贝一份相同的值;而对于引用类型是拷贝其地址,也就是拷贝的对象修改引用类型的变量同样会影响到源对象。
对于深拷贝,任何对象都会被完完整整的拷贝一份,拷贝对象与被拷贝对象不存在任何联系,也就不会互相影响。
如果你需要拷贝的对象中没有引用类型,那么对于Golang而言使用浅拷贝就可以了。
基于序列化和反序列化来实现对象的深度拷贝:
import "encoding/gob"
func deepCopy(dst, src interface{}) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
return err
}
return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
}参考:
边栏推荐
- LeetCode 1673. Find the most competitive subsequence**
- 452. detonate the balloon with the minimum number of arrows
- Yii2 ActiveRecord 使用表关联出现的 id 自动去重问题
- 项目连接不到远程虚拟机The driver has not received any packets from the server.
- Go language channel understanding and use
- Multipass Chinese documentation - Tutorial
- JVM garbage collection mechanism and common garbage collectors
- UUID quick explanation
- BGP基础概念及IBGP基本配置
- Computer screen recording free software GIF and other format videos
猜你喜欢

compiler explorer

【无标题】4555

Blog recommendation | building IOT applications -- Introduction to flip technology stack

How to install mathtype6.9 and related problem solutions in office2016 (word2016)

SLAM卡尔曼滤波&&非线性优化

How word inserts a guide (dot before page number) into a table of contents

Signature verification failed during system application installation

Introduction and basic construction of kubernetes

亿级搜索系统(优酷视频搜索)的基石,如何保障实时数据质量?v2020

On the quality assurance system of youzan search V2021
随机推荐
扎实的基础知识+正确的方法是快速阅读源码的关键
飞利浦 COO 人事变动,将临危受命解决“供应链和产品召回”双重危机
f‘s‘f‘s‘f‘s‘d
Décomposition détaillée du problème de chemin le plus court du graphique
Yum source update
Canvas drawing line break
文件缓存和session怎么处理?
Yii2 ActiveRecord 使用表关联出现的 id 自动去重问题
海贼oj#148.字符串反转
为什么使用 Golang 进行 Web 开发
[network planning] 2.2.3 user server interaction: cookies
Synchronized keyword for concurrent programming
【无标题】测试下啊
阻塞队列 — DelayedWorkQueue源码分析
Dynamic programming classical topic triangle shortest path
Blocking queue - delayedworkqueue source code analysis
How word inserts a guide (dot before page number) into a table of contents
twelve billion three hundred and twenty-four million two hundred and forty-three thousand two hundred and forty-two
String time sorting, sorting time format strings
The JVM determines whether an object can be recycled