dongwei3120 2016-04-19 14:02
浏览 171
已采纳

Go中的不可变字符串

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
}
  • 写回答

1条回答 默认 最新

  • douzhuan1467 2016-04-19 14:31
    关注

    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)))
    }
    

    <kbd>Playground</kbd>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料