前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang-101-hacks(12)——copy

golang-101-hacks(12)——copy

作者头像
羊羽shine
发布2019-06-02 15:04:04
3650
发布2019-06-02 15:04:04
举报
文章被收录于专栏:Golang开发

注:本文是对golang-101-hacks中文翻译。 内置函数copy定义如下:

func copy(dst, src []Type) int 内置函数copy将元素从源切片复制到目标切片。(作为一种特殊情况,它还会将字符串中的字节复制到一个字节片段)切片源和目标可能存在重叠。Copy返回值是copy的总数,即是len(src)和len(dst)的最小值。

看一下基础演示代码,其源切片和目标切片双方不存在重叠

代码语言:javascript
复制
package main

import (
    "fmt"
)

func main() {
    d := make([]int, 3, 5)
    s := []int{2, 2}
    fmt.Println("Before copying (destination slice): ", d)
    fmt.Println("Copy length is: ", copy(d, s))
    fmt.Println("After copying (destination slice): ", d)

    d = make([]int, 3, 5)
    s = []int{2, 2, 2}
    fmt.Println("Before copying (destination slice): ", d)
    fmt.Println("Copy length is: ", copy(d, s))
    fmt.Println("After copying (destination slice): ", d)

    d = make([]int, 3, 5)
    s = []int{2, 2, 2, 2}
    fmt.Println("Before copying (destination slice): ", d)
    fmt.Println("Copy length is: ", copy(d, s))
    fmt.Println("After copying (destination slice): ", d)

}

上面的代码中,目标切片长度是3,源切片长度分别是2,3,4,查看运行结果:

代码语言:javascript
复制
Before copying (destination slice):  [0 0 0]
Copy length is:  2
After copying (destination slice):  [2 2 0]
Before copying (destination slice):  [0 0 0]
Copy length is:  3
After copying (destination slice):  [2 2 2]
Before copying (destination slice):  [0 0 0]
Copy length is:  3
After copying (destination slice):  [2 2 2]

由此可见复制元素的最终个数是源切片和目标切片两者间的最小长度。

We can make sure the number of copied elements is indeed the minimum length of source and destination slices. 接着看存在重叠元素的复制演示程序:

代码语言:javascript
复制
package main

import (
    "fmt"
)

func main() {
    d := []int{1, 2, 3}
    s := d[1:]

    fmt.Println("Before copying: ", "source is: ", s, "destination is: ", d)
    fmt.Println(copy(d, s))
    fmt.Println("After copying: ", "source is: ", s, "destination is: ", d)

    s = []int{1, 2, 3}
    d = s[1:]

    fmt.Println("Before copying: ", "source is: ", s, "destination is: ", d)
    fmt.Println(copy(d, s))
    fmt.Println("After copying: ", "source is: ", s, "destination is: ", d)
}

结果如下:

代码语言:javascript
复制
Before copying:  source is:  [2 3] destination is:  [1 2 3]
2
After copying:  source is:  [3 3] destination is:  [2 3 3]
Before copying:  source is:  [1 2 3] destination is:  [2 3]
2
After copying:  source is:  [1 1 2] destination is:  [1 2]

通过输出结果可以看出,无论源切片是否在目标切片之前,结果总是与预期一致。因此可以人为:源切片中的数据复制到一个临时切片,然后将元素从临时切片复制到目标切片。 Through the output, we can see no matter the source slice is ahead of destination or not, the result is always as expected. You can think the implementation is like this: the data from source slice are copied to a temporary place first, then the elements are copied from temporary to destination slice.

“copy”要求源切片和目标切片的数据类型需要一致性,但存在例外是源切片是字符串,而目标片是“[]byte”:

代码语言:javascript
复制
package main

import (
    "fmt"
)

func main() {
    d := make([]byte, 20, 30)
    fmt.Println(copy(d, "Hello, 中国"))
    fmt.Println(string(d))
} 

输出结果如下

代码语言:javascript
复制
13
Hello, 中国

参考: copy() behavior when overlapping.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.06.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档