如何理解go语言字符串不能修改
先来看一下官方对string
的定义:
// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.
type string string
golang里面字符串是不能修改的,但是,如何理解呢?
比如:
s:="hello"
s="world"
s=s+"wprld"
上面代码都能修改呀?是哪里理解不对吗?
可以这样理解: 原来是s:="hello",把他变为s="world",那么s的指向存储实际字符串的内存地址str变了。变为world的地址了,但是s自己本身的地址没有变