is anyone able to explain me why the address of &c1.name
is the same after being changed in function changeMe()
. I thought strings are immutable in Go.
package main
import "fmt"
type customer struct {
name string
age int
}
func main() {
c1 := customer{"Todd", 44}
fmt.Println(&c1.name) // 0x8201e4120
changeMe(&c1)
fmt.Println(c1) // {Rocky 44}
fmt.Println(&c1.name) // 0x8201e4120
}
func changeMe(z *customer) {
fmt.Println(z) // &{Todd 44}
fmt.Println(&z.name) // 0x8201e4120
z.name = "Rocky"
fmt.Println(z) // &{Rocky 44}
fmt.Println(&z.name) // 0x8201e4120
}