Home  >  Article  >  Backend Development  >  What does string mean in go language?

What does string mean in go language?

王林
王林Original
2020-12-17 11:27:584641browse

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.

What does string mean in go language?

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.

What does string mean in go language?

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:

What does string mean in go language?

What does string mean in go language?

## Result:

a=H 
b=e 
str=Mello,World

rune 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:

What does string mean in go language?

Result:

strLen=12 
str2ByteSlice=12 
str2RuneSlice=8

From the result, we can see

1, the length of the string and the byte slice The length is consistent

2. 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

What does string mean in go language?

Result:

str2RuneSlice[0]=H 
str2RuneSlice[1]=e 
str2RuneSlice[2]=l 
str2RuneSlice[3]=l 
str2RuneSlice[4]=o 
str2RuneSlice[5]=, 
str2RuneSlice[6]=世 
str2RuneSlice[7]=界

Related recommendations:

golang tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn