How to use the rune method in the Go language. Many novices are not very clear about this. In order to help you solve this problem, the editor below will explain it in detail for you. People who have needs in this regard can learn it. , hope you can gain something.
1. Rune type
The rune type is a basic type in the Go language. It is actually an alias of uint32. It is mainly used to represent a character type that is greater than one byte and less than or equal to 4 In the case of 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
Remarks: Excerpted from https://golang.org/pkg/builtin/#rune
Note: A Chinese character can be represented by three bytes, so rune has better support for strings with Chinese characters.
2.byte type
The byte type is an alias for uint8, which represents a byte. The definition is 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
Remarks: Excerpted from https://golang.org/pkg/builtin/#byte
3. Example
Result analysis: Through the above output results, We can see that 1). For English strings, whether the rune type or the byte type is used, the length and value of the string are the same. 2). For Chinese characters, the rune type operation is much more friendly than the byte type operation. We can directly take out the corresponding number of Chinese characters through the [:] operation, but the byte is garbled??.
Was reading the above content helpful to you? If you want to learn more about related knowledge or read more related articles, please pay attention to the Programming Notes Industry Information Channel. Thank you for your support of Programming Notes.