package main
import "fmt"
func main() {
ages := make([]int, 3, 7)
fmt.Printf("original slice memory address is %p\n", ages)
modify(ages)
fmt.Println(ages)
}
func modify(ages []int) {
fmt.Printf("inside function the address of slice is %p\n", ages)
ages[0] = 1
ages = append(ages, 1)
fmt.Printf(" after append address %p\n", ages)
fmt.Printf("after append value %v\n", ages)
}
the result is :
original slice memory address is 0xc0000ac040
inside the function the address of slice is 0xc0000ac040
after append address 0xc0000ac040
after append value [1 0 0 1]
[1 0 0]
I'm wondering why the address is the same. But it still doesn't change the value of the slice.
I've read some articles said it
manipulate the copy of the ages, not the "real" ages.
And I also know that I can pass *ages to the function. And it works.
I just can't understand the address is the same. But the behavior is different.