Preface:
In the previous article, we initially learned the character strings of the Go language. In this article, we will continue to learn other content of the character strings. If we are not in a hurry, we will learn the go Language slowly. In one breath, we cannot eat big fat people. I also hope that my colleagues who like this series will continue to study this series after reading this series and don't have any pressure. It's just news, you can know how much it is. When you finish reading it all, you may be "fat. So let's continue learning ~
An Immutable string
In the previous article, we mentioned this feature at the beginning, but I forgot to mention it later. I am very sorry (= |) for this, so I just made up it today. It is easier to use examples. See the following:
The example here provides a good explanation of the unmutable character string. When you delete the 13-line comment, an error is returned when compiling the file because the character string content cannot be modified. Many beginners may think that line 1 does not change the string content? This understanding is wrong. Line 1 only points the variable cat to another memory address. The original string has not changed. What you changed is the variable address.
Some people may wonder if the 13th rows are single quotes? In the go language, single quotes indicate a Unicode character, which is OK. If you do not believe it, try the following:
Here, a 5-byte array is used for storage, and the 'C' character is placed in the first position, and the result is printed:
The first position of the array is filled with 99. That's right. This 99 corresponds to the ASCII value of the 'C' character. The default value is 0 for other locations. However, after removing the comments from the 7th rows, when we assign a Chinese character to a certain position in the array, the compilation will fail. Because, in Linux VI, the default encoding method is UTF-8, so the Chinese encoding needs 3 bytes. In this byte array, each array element has only one byte capacity, so Chinese characters are not allowed. What should we do if we have to put Chinese characters? See the following:
Replace the byte array with an array of the rune type. The reason is that rune has a 32-bit length, which is enough to put down three Chinese Characters in bytes.
Binary string Traversal
In the go language, you need to traverse a string and access each character in a way similar to other languages. Of course, the for loop is used, and the for loop is similar to other languages, so we will not discuss it separately later. Here we will use the learning for loop directly:
If this is the case above, you will find that a bunch of garbled characters are printed at the end.Code% C in indicates formatting into characters, so that each character can be printed. However, unfortunately, we failed. If you assign a value to all English letters, you will find that it can be printed again. It is obvious that character encoding is at stake here. Because the internal structure of the string is a byte array, which has been seen in the previous article, the Len () function is called by default. The obtained string length is actually the number of elements in the byte array, so every element you take out is a byte, if it is an English character, it does not matter, it is a byte, but when it is Chinese, because it is a UTF-8 encoding, so, one Chinese character requires three bytes for representation. Now you only retrieve one of the three bytes. You can imagine that it can only be a garbled text.
Now, you can think of a solution. Convert the byte array in the string into a rune array. Yes, see the following:
In addition to this method, you can also use the iteration syntax of the Go language:
Concatenation of three strings
In the go language, the common String concatenation can be done directly with the + sign:
However, to improve the performance, you can import the bytes package as follows:
This is today .....