I am confused about pass by value in Go. I am doing this;
package main
import (
"fmt"
)
type point struct {
x int
list []int
}
func main() {
p := point{20, []int{1, 2, 3, 4, 5}}
fmt.Printf("Address: %p, Value: %v\n", &p, p)
passByValue(p, 1)
fmt.Printf("Address: %p, Value: %v\n", &p, p)
}
func passByValue(copyOfP point, i int) {
copyOfP.list = append(copyOfP.list[:i], copyOfP.list[i+1:]...)
fmt.Printf("From passByValue Address: %p, Value: %v\n", ©OfP, copyOfP)
}
Output:
Original p Address: 0xc00000c080, Value: {20 [1 2 3 4 5]}
passByValue copyOfP Address: 0xc00000c0c0, Value: {20 [1 3 4 5]}
Original p Address: 0xc00000c080, Value: {20 [1 3 4 5 5]}
shouldn't copyOfP
be a copy of p
and not reflect on original p
what so ever.
Whats happening here?