How to use rune method in Go language
How to use the rune method in Go language, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.
1. rune type
The rune type is a basic type in Go. In fact, it is an alias of uint32. It is mainly used to indicate that a character type is greater than one byte and less than or equal to 4 bytes, especially Chinese characters. The definition is as follows:
rune is an alias for int32 and is equivalent to int32 in all ways. It is used, by convention, to distinguish character values from integer values.
type rune = int32
Note: Taken from golang.org/pkg/builtin/#rune
Note: A Chinese character can only be represented by three bytes, so rune supports better when the character string is Chinese characters.
2. byte type
byte type is an alias for uint8, representing a byte, defined as follows:
byte is an alias for uint8 and is equivalent to uint8 in all ways. It is used, by convention, to distinguish byte values from 8-bit unsigned integer values.
type byte = uint8
Note: Taken from golang.org/pkg/builtin/#byte
3. example
Result analysis: From the output results above, we can see that 1). For English strings, no matter whether they are of type rune or byte, the length or value of the string is the same. 2). For Chinese characters, rune-type operations are much more friendly than byte-type operations. We can directly extract the corresponding number of Chinese characters through [:] operation, while byte is extracted with random code??.
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.