如果len不改变,为什么数组/切片的指针在Go中改变?

I made a program to learn more about arrays and slices, in this example I just change the value of a fix position. When I look to the pointer address it changed after each change. Why this happen?

Source:

func main() {
    tstSlice2()

}

func tstSlice2() {
    var meuSlice = make([]int, 1, 2)
    meuSlice[0] = 1
    printSlice(meuSlice, "meuSlice")
    meuSlice[0] = 2
    printSlice(meuSlice, "meuSlice")
    meuSlice[0] = 3
    printSlice(meuSlice, "meuSlice")
}

func printSlice(meuSlice []int, sliceName string) {
    fmt.Printf("%v> %v - %v - %v - %p 
", sliceName, len(meuSlice), cap(meuSlice), meuSlice, &meuSlice)
}

Output:

▶ go run .\src\array_e_slices\slice.go    
meuSlice> 1 - 2 - [1] - 0xc0420503e0  
meuSlice> 1 - 2 - [2] - 0xc042050440  
meuSlice> 1 - 2 - [3] - 0xc042050480 

What you are passing in printSlice function is not a slice but a slice header, that is describing the underlying array. When you pass a slice to a function, a copy of the header is made, since in go elements are always passed by value.

In a function call, the function value and arguments are evaluated in the usual order. After they are evaluated, the parameters of the call are passed by value to the function and the called function begins execution. The return parameters of the function are passed by value back to the calling function when the function returns.

If you want to inspect what's inside of this header, check reflect.SliceHeader type.

An example of the code:

package main

import (
    "fmt"
    "reflect"
    "unsafe"
)

func main() {
    tstSlice2()

}

func tstSlice2() {
    var meuSlice = make([]int, 1, 2)
    meuSlice[0] = 1
    printSlice(meuSlice, "meuSlice")
    meuSlice[0] = 2
    printSlice(meuSlice, "meuSlice")
    meuSlice[0] = 3
    printSlice(meuSlice, "meuSlice")
}

func printSlice(meuSlice []int, sliceName string) {
    hdr := (*reflect.SliceHeader)(unsafe.Pointer(&meuSlice))
    fmt.Printf("%v> %v - %v - %v - %p - array: %v
", sliceName, len(meuSlice), cap(meuSlice), meuSlice, &meuSlice, hdr.Data)

}

Output:

meuSlice> 1 - 2 - [1] - 0xc42000a060 - array: 842350559504
meuSlice> 1 - 2 - [2] - 0xc42000a0a0 - array: 842350559504
meuSlice> 1 - 2 - [3] - 0xc42000a0e0 - array: 842350559504

Go playground: https://play.golang.org/p/x2OoIHgU9St

You can find more information in the blog post Go Slices: usage and internals (especially Slice internals section)

A slice is a descriptor of an array segment. It consists of a pointer to the array, the length of the segment, and its capacity (the maximum length of the segment).

Slice

What you are passing to your function is a value of this struct, not the underlying array itself. To do that, you'd need to use *[]int argument, as mentioned in other answers.

Because you are passing each time a copy of slice. While underlying array is not copied the slice does. I think in principal you can treat slice as a struct:

type Slice struct {
   pointer PointerToArray
   StartIndex int
   Len int
}

Code below should print the same pointer:

func tstSlice2() {
    var meuSlice = make([]int, 1, 2)
    meuSlice[0] = 1
    printSlice(&meuSlice, "meuSlice")
    meuSlice[0] = 2
    printSlice(&meuSlice, "meuSlice")
    meuSlice[0] = 3
    printSlice(&meuSlice, "meuSlice")
}

func printSlice(meuSlice *[]int, sliceName string) {
    fmt.Printf("%v> %v - %v - %v - %p 
", sliceName, len(*meuSlice), cap(*meuSlice), *meuSlice, meuSlice)
}

A slice value contains a pointer to a backing array, length and capacity. See Go Slices: usage and internals for all of the details.

The program prints the address of the slice menuSlice. The address of the argument is not the same from call to call.

It looks like your goal is to print the address of the slice's backing array. To do this, print the address of the first slice element. The address of the first element is the same as the address of the entire backing array.

func printSlice(meuSlice []int, sliceName string) {
    fmt.Printf("%v> %v - %v - %v - %p 
", sliceName, len(meuSlice), cap(meuSlice), meuSlice, &meuSlice[0])
}

The output of this program is:

meuSlice> 1 - 2 - [1] - 0x10414020 
meuSlice> 1 - 2 - [2] - 0x10414020 
meuSlice> 1 - 2 - [3] - 0x10414020 

playground example

The above code panics on nil and zero length slices. To protect against the panic, check the length of the slice before take the address of the first element:

func printSlice(meuSlice []int, sliceName string) {
    var p *int
    if len(meuSlice) > 0 {
        p = &meuSlice[0]
    }
    fmt.Printf("%v> %v - %v - %v - %p 
", sliceName, len(meuSlice), cap(meuSlice), meuSlice, p)
}

playground example