为什么在Go语言中修改字符串不会分配额外的空间?

huangapple go评论75阅读模式
英文:

Why modifying a String in Go is NOT allocating extra space?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习Go语言,试图理解Go中的字符串是如何工作的。我有一个重要的疑问,为什么在我的示例中,将str从"black"更改为"Orange"时,并没有改变str的地址。我注意到,在使用str_cpy进行复制时,地址会发生变化。

提前感谢。

代码:

func main(){
  // 将str设置为Black
  var str string = "Black"
  log.Println("String is set to: ", str)
  log.Println("Current address of String is: ", &str)

  // 将str设置为black
  log.Println("Testing Mutability...")
  slice := []rune(str)
  slice[0] = 'b'
  str = string(slice)
  log.Println("String after mutation is...", str)
  log.Println("Current address of String is: ", &str)
 
  // 复制str,str_cpy的预期值为black
  str_cpy := str
  log.Println("StringCOPY is set to: ", str_cpy)
  log.Println("Current address of StringCOPY is: ", &str_cpy)

  // 将str从black更改为Orange
  str = "Orange"
  log.Println("String now is...", str)
  log.Println("Current address of String is: ", &str)

  changeusingPointer(&str)
  log.Println("String was changed using pointer to: ", str)
  log.Println("Current address of String is: ", &str)
  
}

func changeusingPointer(s *string){
  log.Println("s points to ", s)
  *s = "White"
}

我得到的输出是:

2023/02/18 10:39:38 String is set to:  Black
2023/02/18 10:39:38 Current address of String is:  0xc000014060
2023/02/18 10:39:38 Testing Mutability...
2023/02/18 10:39:38 String after mutation is... black
2023/02/18 10:39:38 Current address of String is:  0xc000014060
2023/02/18 10:39:38 StringCOPY is set to:  black
2023/02/18 10:39:38 Current address of StringCOPY is:  0xc000014090
2023/02/18 10:39:38 String now is... Orange
2023/02/18 10:39:38 Current address of String is:  0xc000014060
2023/02/18 10:39:38 s points to  0xc000014060
2023/02/18 10:39:38 String was changed using pointer to:  White
2023/02/18 10:39:38 Current address of String is:  0xc000014060

我期望在将字符串str的值从"black"更改为"Orange"时,字符串str的地址会发生变化。

英文:

I'm new to Go and trying to understand how Strings work in Go. My major doubt is, why changing str from "black" to "Orange" in my example NOT change the address of str. I notice the address changes while making a copy with str_cpy.

Thanks in advance.

Code:

func main(){
  // set str as Black
  var str string = "Black"
  log.Println("String is set to: ", str)
  log.Println("Current address of String is: ", &str)

  //set str as black
  log.Println("Testing Mutability...")
  slice := []rune(str)
  slice[0] = 'b'
  str = string(slice)
  log.Println("String after mutation is...", str)
  log.Println("Current address of String is: ", &str)
 
  //make a copy of str, str_cpy expected value: black
  str_cpy := str
  log.Println("StringCOPY is set to: ", str_cpy)
  log.Println("Current address of StringCOPY is: ", &str_cpy)

  //change str from black to Orange
  str = "Orange"
  log.Println("String now is...", str)
  log.Println("Current address of String is: ", &str)

  changeusingPointer(&str)
  log.Println("String was changed using pointer to: ", str)
  log.Println("Current address of String is: ", &str)
  
}

func changeusingPointer(s *string){
  log.Println("s points to ", s)
  *s = "White"
}

The Output I get is:

2023/02/18 10:39:38 String is set to:  Black
2023/02/18 10:39:38 Current address of String is:  0xc000014060
2023/02/18 10:39:38 Testing Mutability...
2023/02/18 10:39:38 String after mutation is... black
2023/02/18 10:39:38 Current address of String is:  0xc000014060
2023/02/18 10:39:38 StringCOPY is set to:  black
2023/02/18 10:39:38 Current address of StringCOPY is:  0xc000014090
2023/02/18 10:39:38 String now is... Orange
2023/02/18 10:39:38 Current address of String is:  0xc000014060
2023/02/18 10:39:38 s points to  0xc000014060
2023/02/18 10:39:38 String was changed using pointer to:  White
2023/02/18 10:39:38 Current address of String is:  0xc000014060

I expected the address of string str to change while changing its value from "black" to "Orange" .

答案1

得分: 1

一个字符串是一个包含两个元素的结构:指向包含字符串的数组的指针和字符串的长度。所以当你声明 str="Orange" 时,发生的情况类似于以下内容:

str=internalString{ arr: <指向数组 "Orange" 的指针>, len: 6}

当你给 str 赋一个新值,比如 "Black",变成了:

str=internalString{ arr: <指向数组 "Black" 的指针>, len: 5}

str 的地址不会改变。

英文:

A string is a structure containing two elements: pointer to the array containing the string, and the string length. So when you declare str=&quot;Orange&quot;, what happens resembles the following:

str=internalString{ arr: &lt;pointer to array &quot;Orange&quot;&gt;, len: 6}

When you assign a new value to str, like "Black", this becomes:

str=internalString{ arr: &lt;pointer to array &quot;Black&quot;&gt;, len: 5}

Address of str does not change.

答案2

得分: 0

当你创建str_copy时,你复制了值并将其放在其他地方,所以指针应该改变,但是当你改变str的值时,数组指针保持不变,只有值发生改变。

英文:

when you create str_copy you copy the value and put it some where else so the pointer should change but when you change the value of str the array pointer remains constant just the values change

huangapple
  • 本文由 发表于 2023年2月18日 14:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定