This article is introduced by the golang tutorial column to introduce to you the issue of string type modification in golang. You may be wondering. It is normal for us to modify strings in daily development. Why do you hear this? What about strings in Go that cannot be modified? The following will be explained in detail with examples. I hope it will be helpful to friends in need!
When you come into contact with a language like Go, you may often hear this sentence. You may be wondering about the fact that strings cannot be modified. It is normal for us to modify strings in daily development. Why do we say that strings in Go cannot be modified?
This article will demonstrate to you through actual cases why strings in Go cannot be modified.
Before demonstrating this problem, let us first give a rough demonstration of the basic knowledge of string types, so that everyone can further understand the problem.
String is a data type used to represent characters. When using, use " " to enclose the character content. For example, the following form:
package main import "fmt" func main() { var str string = "Hello World!" }
In Go, strings are usually defined in three ways:
// 第一种(全量定义) var 变量名称 string = "字符串内容" // 类型推导 var 变量名称 = "字符串内容" // 短标记(只适用于局部变量) 变量名称 := "字符串内容"
The definition of strings can also be defined in bytes. The methods listed here are the most common ones.
Strings in Go conform to the Unicode standard and are encoded in UTF-8. The bottom layer of the string is actually composed of bytes (will be explained in detail later). Use the following example to print and view the specific byte content:
s := "Hello World!" for _, v := range s { fmt.Print(v) fmt.Print("\t") } // 72 101 108 108 111 32 87 111 114 108 100 33
The content printed by the above code is the bytecode represented by each character.
Through the above rough demonstration, we have a basic understanding of strings. As for strings that cannot be modified, you may be wondering. It is normal for us to reassign strings in daily development. Why do we say that strings in Go cannot be modified?
In fact, we need to correct this statement here. Modifying a string is not equivalent to reassignment. The commonly used method in development is actually a concept of reassignment.
str := "Hello World!" // 重新赋值 str = "Hello Go!" // 字符串修改 str[0] = "I"
We usually hear that it cannot be modified, which actually refers to the second way of the above code. And if modified in this way, an error will be reported: :cannot assign to s [0] (value of type byte)
Back to the topic, why can’t strings in Go be modified by subscripting?
This is because the string data structure in Go is a structure composed of a pointer and a length, and a slice pointed to by the pointer is the real string value. The source code in Go has such a definition:
type stringStruct struct { str unsafe.Pointer // 指向一个byte类型的切片指针 len int // 字符串的长度 }
It is precisely because the bottom layer is a slice of [] byte type. When we use subscripts to modify the value, this time Assigning a character content to the byte type is definitely not allowed. But we can access the corresponding byte value through subscripting.
fmt.Println(s[0]) // output:72
So what should we do if we want to modify the value through subscripting? At this time, you need to define it through slicing and then convert it into a string.
package main import ( "fmt" ) func main() { s1 := []byte{72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33} fmt.Println(string(s1)) // 将"H"修改为l s1[0] = 108 fmt.Println(string(s1)) } // output: Hello World! lello World!
The above analyzes why strings cannot be assigned using subscripts. Let’s go back and answer the assignment methods in daily development.
package main import ( "fmt" ) func main() { // 声明一个字符串,并给与初始值 s := "Hello World!" // 对变量 s 进行重新赋值 s := "Hello Go!" }
Then why can the string be reassigned in this scenario?
This is because at the bottom of Go, a slice of type [] byte {} is actually newly created, pointing the pointer in the variable s to the new memory space address (that is, Hello Go! here! ). The original Hello World! memory space will be reclaimed with the garbage collection mechanism.
Maybe everyone will consider why an ordinary string is so complicated to design and needs to use pointers. I haven't found any official documentation yet.
My personal guess is that when encountering a very long character, this makes string very lightweight and can be easily transferred without worrying about memory copying. Although in Go, whether it is a reference type or a value type parameter passing, it is passed by value. But pointers are obviously more memory efficient than passing by value.
The above is the detailed content of Why can't Golang string type be modified?. For more information, please follow other related articles on the PHP Chinese website!