Home >Backend Development >Golang >What does string mean in go language?
In the Go language, string is a string. Its essence is a [[]byte], so they can be converted to each other. The length of the byte array is the length of the string. Once the value of a string is determined, it cannot be modified.
The environment of this article: Windows 10 system, Go 1.11.2 version, this article is applicable to all brands of computers.
(Learning video sharing: Programming video)
The essence of Go language String is a []byte, so they can be converted to each other. The length of the byte array is The length of the string.
Result:
a=H b=e str=Mello,World
Once the value of the string is specified, it cannot be modified. If you want to modify it, you can first replace the string with slice.
//当试图去修改str时候 str[0] = 'M'
The following error message will appear:
## Result:a=H b=e str=Mello,Worldrune represents utf8 Character, a rune character consists of one or more bytes. What is the difference between rune and string length? You can refer to the following example: Result:
strLen=12 str2ByteSlice=12 str2RuneSlice=8From the result, we can see 1, the length of the string and the byte slice The length is consistent2. The length of the string is larger than the length of the rune slice, which means that a Chinese character needs to occupy multiple bytes, here it is 3, so there is the result of str2RuneSlice = 8 Then we can traverse this rune slice and append the following code Result:
str2RuneSlice[0]=H str2RuneSlice[1]=e str2RuneSlice[2]=l str2RuneSlice[3]=l str2RuneSlice[4]=o str2RuneSlice[5]=, str2RuneSlice[6]=世 str2RuneSlice[7]=界Related recommendations:
The above is the detailed content of What does string mean in go language?. For more information, please follow other related articles on the PHP Chinese website!