No, it should not operate that way. Remember that all function calls in Go pass arguments by value. A slice is actually a value somewhat analogous to
type SliceOfT struct { Data *T Len int Cap int } When you pass a slice to a function, it’s this struct that gets passed in. Notice that the length and capacity are values, but the data pointer is a pointer. This is why you can change the contents of a slice from within a function, but not its length or capacity. func modify(x []int) { x[0] = 1 x = append(x, 4) } func main() { x := []int{2, 3} modify(x) fmt.Println(x) // prints [1 3] } copy() is a function just like any other Go function; just because it’s built in does not mean it follows any special semantics. The current behavior of copy() allows you to do interesting things with copy(); for example: type buffer struct { data []byte pos int } // … func (b *buffer) append(val …byte) { for len(val) > 0 { n := copy(b.data[b.pos:], val) b.pos += n if b.pos == len(b.data) { b.flush() b.pos = 0 } val = val[n:] } } No matter what the value of b.pos is, the copy() will still copy in at least 0 elements into b.data without altering b.data itself. If copy() altered b.data somehow, this loop would not work at all and would need to be rewritten somehow. As another example: a := make([]byte, 50) b := make([]byte, 32) copy(b[:16], a[4:]) copy(b[16:], a[25:]) to fill in b with different fragments of a. > On Oct 2, 2016, at 1:11 AM, 高橋誠二 <timaki...@gmail.com> wrote: > > As official doc explains, copy to zero length slice results to be empty. > > package main > > import "fmt" > > func main() { > src := []int{1, 2, 3} > dst := []int{} > fmt.Println(len(dst)) > copy(dst, src) > fmt.Println(src) > fmt.Println(dst) > // [1 2 3] > // [] > > src2 := []int{1, 2, 3} > dst2 := make([]int, len(src)) > copy(dst2, src2) > fmt.Println(src2) > fmt.Println(dst2) > // [1 2 3] > // [1 2 3] > } > > > But it seems unusual, is it should be like following? > package main > > import "fmt" > > func main() { > src := []int{1, 2, 3} > dst := []int{} > fmt.Println(len(dst)) > copy(dst, src) > fmt.Println(src) > fmt.Println(dst) > // [1 2 3] > // [1 2 3] > } > > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.