Let's go (6) together)

Source: Internet
Author: User
Preface:

As always, I will continue to learn the go language, but before continuing to learn, I will be allowed to talk nonsense. It is very important to stick to one thing recently, and it is hard to stick to one thing recently. Despite the basics of the article, I still want to stick to it. If you have finished the nonsense, continue.

 

A string of the Go Language

Like most object-oriented programming languages, go also has the string type, except that it is a value type different from the string type in other languages such as Java. Note that the declared keywords are string, all in lower case. (especially for Java programmers, C # programmers steal music ......). In addition, it also has a feature that is immutable. here we need to note that the immutable character string itself is not immutable. You can see it later in the example. In the go language, the structure of the string type is as follows:

1 struct String2 {3     byte*    str;4     int32    len;5 };

The above structure can be found in the runtime. h header file in the source code of the Go language.
From the above structure, we can see that the string type is actually composed of a byte pointer and a variable representing the string length of the int32 type. Here, the byte is the alias of uint8. In essence, it represents an 8-bit unsigned integer. Therefore, in essence, a string on a computer is actually a number. However, the numbers are mapped to corresponding characters using different encoding methods. In addition, the Go language uses the UTF-8 encoding method. If you still don't understand the so-called encoding method, check it on the Internet. Because there are many knowledge points involved, and today we only focus on the go language, so I won't go into detail here.

The above structure is the structure of the C language in the runtime of the Go language. Therefore, you can understand the string type in the go language as the above structure internally, it's not necessary to figure it out now. It's about the underlying implementation of the go language. Now we only need to use it. Of course, you can also dig from the bottom layer if you are interested.

To make things more fun, we can also simulate the above structure at the go language level. Of course, it is meaningless in reality. Let's look at the following:

 

 

In the main function, first declare and define an animal string, convert it into a byte array, and pass its address to our custom string struct, but isn't the first parameter of the string struct a pointer type? Actually, the pointer variable is the variable used to store the memory address of this type. when it receives the first address of a byte array, it can control the array. Therefore, when we pass the address of the byte array of B in front of the string struct, we can actually control this array. Is it dizzy? Hey, it's normal. When I first learned the C language, I never understood the pointer. Later, I got used to it. Let's look at 30 rows. This print is a function defined by us to print this struct. When we pass in the cat variable of the string struct type, we traverse the pointer variable of the struct in 16th rows cyclically, print out each of its bytes. Since the go language cannot directly perform pointer operations like the C language, you need to introduce the unsafe package and use it for computation. I will not detail it here, if you do not need to know the documentation, You can query the documentation, but it does not make much sense for beginners. In addition, go does not recommend direct pointer operations. Otherwise, you can use C directly. After the above discussion, the byte is converted to a string and printed, so the byte array is restored to a string.

After reading the above section, it is estimated that some readers will scold me. The go language is much more troublesome than the C language. Please be calm. The above is rarely used in actual development. Otherwise, it would be better to directly use the C language. Here I write a lot of it, but I just want to explain its internal structure, by taking the go structure into practice, you don't have to worry about it if you don't understand it at all. Maybe we will be able to understand it after a long time as we study it in depth.

 

Binary string operation

After learning about the basic information about the string, let's take a look at the operations on the string. In the previous section, we have obtained the length of the string through the Len function. However, the result is not the number of characters in the string, which seems to be different from other languages, such as Java. However, when you assign an English character to it, it seems that the result is the number of characters. However, if you assign a value to it for Chinese, it may be incorrect. If you do not believe it, try it. Here I switched the development platform to Linux, because the character set operations on the UTF-8 are not very convenient at the windows command prompt. See the example below:

 

The final result is:

The result is 12. Why is the result true? The reason is that in the go language, the characters are UTF-8 encoded. One English character is counted as one byte, And the other Chinese character is counted as two bytes. So what if we have to get the number of characters? You can convert a string to the [] Rune type:

Rune is actually an alias of int32, so here it is actually converting string into a 32-bit integer array and saving it to the Unicode of the corresponding character, in this way, several characters correspond to several Unicode characters in the array respectively. Therefore, the length is 2. Of course, you can print and see what Unicode is:

Next, let's take it easy. We all know that arrays can be sliced in Python, or in go. As follows:

Final result:

Today, I feel that the article is getting longer and longer, and there is no way to do it. This will happen with deep learning, but I will try my best to shorten the length of each article. Hope to help you ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.