Golang Copy()方法学习

前言

主要是涉及到深浅拷贝相关的,但是在看的一个资料过程中发现他有错…并且一系列,复制粘贴他的,也都错了。

错误文章指路

在这里插入图片描述
在这里插入图片描述

很显然,Copy是深拷贝啊!!!

Copy功能

copy的代码很少,如下:


// The copy built-in function copies elements from a source slice into a
// destination slice. (As a special case, it also will copy bytes from a
// string to a slice of bytes.) The source and destination may overlap. Copy
// returns the number of elements copied, which will be the minimum of
// len(src) and len(dst).
func copy(dst, src []Type) int

一共就一个dst,一个src,一共俩切片。

功能上,简单来说就是:实现切片的拷贝。需要注意的是,这里的拷贝是深拷贝,但1、目标切片需要提前初始化。2、只能直接拷一层。

官方文档在https://golangbyexample.com/copy-function-in-golang/

其中介绍如下:
If the length of src is greater than the length of dst, then the number of elements copied is the length of dst

If the length of dst is greater than length of src, then number of elements copied is length of src

Basically the number of elements copied is minimum of length of (src, dst).
即对应的这篇文章所说的情况1——https://www.jianshu.com/p/be07bee40b08

官方文档中的下一句:
Also to note that once the copy is done then any change in dst will not reflect in src and vice versa unless both src and dst refer to the same slice.

对应的即使此文中的情况2——https://www.jianshu.com/p/be07bee40b08

官方文档之后举例了,可以用copy自己拷贝到自己的情况,这个在网络教程中没怎么搜到。

package main

import "fmt"

func main() {
    src := []int{1, 2, 3, 4, 5}
    numberOfElementsCopied := copy(src, src[3:])

    fmt.Printf("Number Of Elements Copied: %d\n", numberOfElementsCopied)
    fmt.Printf("src: %v\n", src)
}

总结

这个没什么多的内容,但就是说,官方文档比来回cv的那些教程高大上一万倍。。。
官方文档:https://golangbyexample.com/copy-function-in-golang/

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Golang中,可以使用标准库中的io包的Copy函数来实现文件的copy操作。Copy函数的定义如下: ```go func Copy(dst Writer, src Reader) (written int64, err error) ``` 其中,dst是目标文件的写入器,src是源文件的读取器。该函数会将源文件的内容复制到目标文件中,并返回复制的字节数和可能出现的错误。\[1\] 除了使用Copy函数,还可以通过序列化和反序列化来实现对象的深度拷贝。可以使用encoding/gob包来进行序列化和反序列化操作。下面是一个基于序列化和反序列化的深度拷贝函数的示例: ```go 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) } ``` 该函数接受两个参数,dst是目标对象,src是源对象。它会将源对象进行序列化,然后再进行反序列化,最终将结果赋值给目标对象,实现深度拷贝。\[2\] 在Golang中,文件的copy、读取和写入是非常基础和常用的操作。除了使用Copy函数外,还可以使用os包和bufio包等工具来进行文件操作。在进行文件操作时,需要注意正确地打开和关闭文件句柄,以避免资源泄漏和其他问题。\[3\] #### 引用[.reference_title] - *1* *3* [Golang基础之文件的copy与文件的读写](https://blog.csdn.net/SMILY12138/article/details/129952198)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Golang中的深拷贝与浅拷贝](https://blog.csdn.net/Alen_xiaoxin/article/details/124913809)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值