如果每隔1米可以种一颗树,那么10米的可以种几颗树?

——

go 中的copy

12 Oct 2016

func copy(dst, src []Type) int
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).
GO 中内置函数 copy 使用时需要注意的是当 source 和 destination 长度不同时的两种情况。
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]