复制功能如何工作?

How does the copy function work?

根据文档,我不明白 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

这里是如何使用内置复制功能的基本示例:

// The source slice
source := []int{1, 2, 3,}

// The destination slice is where things will be copied to
destination := make([]int, len(source))

// The copy function returns a count of how many elements are copied from the source slice to the destination slice
copies := copy(destination, source)

// Some printing
fmt.Printf("copied %d elements\n", copies)  // Outputs: copied 3 elements
fmt.Println("destination slice:", destination)  // Outputs: destination slice: [1 2 3]

Example Playground


如果destination切片长度为nsource切片长度为m,且n<m 目标切片将填充 source 切片的前 n 个元素:

source := []int{1, 2, 3,}
destination := make([]int, 1) // <-- Notice the 1
copies := copy(destination, source)
fmt.Printf("copied %d elements\n", copies)  // Outputs: copied 1 elements
fmt.Println("destination slice:", destination)  // Outputs: destination slice: [1]

Example Playground


一个陷阱是,如果您将目标切片声明为切片文字,如下所示:

destination := []int{}

长度将为 0,不会复制任何内容。

该规范还涵盖了内置函数 append()copy()Appending to and copying slices。你应该读一下,很清楚。

让我们分析一下内置的文档copy() function and its uses with simple examples. You can try all the examples on the Go Playground

签名:

func copy(dst, src []Type) int

copy()是一个函数,它有2个参数,一个destination和一个source slice,其元素类型相同。它 returns 类型 int 的数字,这是实际复制的元素数。

The copy built-in function copies elements from a source slice into a destination slice.

copy() 会将 src 切片中的元素复制到 dst 切片中。

src := []int{10, 11, 12, 13, 14}
dst := []int{0, 1, 2, 3, 4}

n := copy(dst, src)
fmt.Println("n =", n, "src =", src, "dst =", dst)

输出:

n = 5 src = [10 11 12 13 14] dst = [10 11 12 13 14]

它复制了所有 5 个元素,复制后目标与源具有相同的元素。

让我们继续这个例子:

dst = []int{0, 1}

n = copy(dst, src)
fmt.Println("n =", n, "src =", src, "dst =", dst)

输出:

n = 2 src = [10 11 12 13 14] dst = [10 11]

只复制了 2 个元素,因为目标只有 2 个元素。

继续:

src = []int{10, 11}
dst = []int{0, 1, 2, 3, 4}

n = copy(dst, src)
fmt.Println("n =", n, "src =", src, "dst =", dst)

输出:

n = 2 src = [10 11] dst = [10 11 2 3 4]

同样,只复制了 2 个元素,但这次是因为源只有 2 个元素。

因此 copy() 只会复制与源或目标一样多的元素,以较小者为准。或者换句话说,源 "provides" 或目标可以 "accomodate" 的数量越多,以较小者为准。

(As a special case, it also will copy bytes from a string to a slice of bytes.)

这意味着源也可以是 string 如果目标是 []byte:

str := "Hello World!"
data := make([]byte, 5)
n = copy(data, str)
fmt.Println("n =", n, "str =", str, "data =", data)
fmt.Printf("data as string: %s\n", data)

输出:

n = 5 str = Hello World! data = [72 101 108 108 111]
data as string: Hello

这次源是 stringcopy() 复制了 string 的 UTF-8 表示的 5 个字节(这就是 Go 在内存中存储字符串的方式)。

The source and destination may overlap.

这意味着 copy() 即使目标是与源切片共享同一底层数组的切片,并且源和目标指定的数组部分具有公共部分(重叠),也能正常工作.

例如:

copy(src, src[1:])
fmt.Println("n =", n, "src =", src)

输出:

n = 4 src = [1 2 3 4 4]

这里我指定了src[1:]作为来源,也就是没有第一个元素的来源(这是一个reslicing)。由于我排除了第一个元素,copy() 的来源有 4 个元素,所以 4 个元素被复制了。结果是元素 "shifted" 到一个减 1 索引(因此第一个元素 0 现在从切片中消失了),最后一个元素没有被触及(因为只复制了 4 个元素).

Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).

我们在上面的例子中看到了这一点。

如果您需要了解有关切片的更多信息:

Go Slices: usage and internals

Arrays, slices (and strings): The mechanics of 'append'