golang-101-hacks(12)——copy

栏目: Go · 发布时间: 5年前

内容简介:注:本文是对内置函数func copy(dst, src []Type) int

注:本文是对 golang-101-hacks 中文翻译。

内置函数 copy 定义如下:

func copy(dst, src []Type) int

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

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

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,查看运行结果:

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.

接着看存在重叠元素的复制演示程序:

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)
}

结果如下:

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”:

package main

import (
    "fmt"
)

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

输出结果如下

13
Hello, 中国

参考:

copy() behavior when overlapping .


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

用户力:需求驱动的产品、运营和商业模式

用户力:需求驱动的产品、运营和商业模式

郝志中 / 机械工业出版社 / 2015-11-1 / 59.00

《用户力:需求驱动的产品、运营和商业模式》从用户需求角度深刻阐释了互联网产品设计、网络运营、商业模式构建的本质与方法论! 本书以“用户需求”为主线,先用逆向思维进行倒推,从本质的角度分析了用户的需求是如何驱动企业的产品设计、网络运营和商业模式构建的,将这三个重要部分进行了系统性和结构化的串联,然后用顺向思维进行铺陈,从实践和方法论的角度总结了企业究竟应该如围绕用户的真实需求来进行产品设计、网......一起来看看 《用户力:需求驱动的产品、运营和商业模式》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具