12

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
}
camabeh
  • 1,122
  • 12
  • 13
  • because `c1` is a unique instance, and its address will not be changed, you can only change its value – simon_xia Apr 19 '16 at 14:06
  • yes, `c1` is not being changed, but it's field `name` is immutable, right? So when I change `name` it should have new address. What If I want store extremly large string, how does the compiler store it, when it still uses same address? – camabeh Apr 19 '16 at 14:18

1 Answers1

17

The immutability of strings is not the same as immutability of variables.

Immutability of strings means that the characters in the string cannot be changed. This holds true for Go. Go makes use of it when slicing strings as shown in the example below.

Variables in Go are always mutable. When a string variable is changed, the internal fields of the variable (pointer and length) are changed. The address of variable never changes.

The example below presents the internals of Go string variable. The first integer is an address to the array of characters and the second is the length.

See the article on internals of string in Go http://research.swtch.com/godata.

package main

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

func main() {
    var x string = "abc"
    fmt.Println(x, &x, (*reflect.StringHeader)(unsafe.Pointer(&x)))
    x = "cde"
    fmt.Println(x, &x, (*reflect.StringHeader)(unsafe.Pointer(&x)))
    x = x[1:]
    fmt.Println(x, &x, (*reflect.StringHeader)(unsafe.Pointer(&x)))
}

Playground

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • 2
    There is also the [StringHeader](https://golang.org/pkg/reflect/#StringHeader) in the reflect package, which reflects the actual string struct layout in [runtime/string.go](https://golang.org/src/runtime/string.go#L215) – JimB Apr 19 '16 at 15:03
  • Thank you. It all makes sense now. – camabeh Apr 19 '16 at 15:17