Golang rune type is the alias for int32, and it is used to indicate than the integer represents the code point. ASCII defines 128 characters, identified by the code points 0–127. When you convert the string to a rune slice, you get the new slice that contains the Unicode code points (runes) of a string.
Byte slice is just like a string, but mutable. For example, you can change each byte or character. This is very efficient for working with file content, either as a text file, binary file, or IO stream from networking.
Rune slice is like the byte slice, except that each index is a character instead of a byte. This is best if you work with text files that have lots of non-ASCII characters, such as Chinese text or math formulas ∑ or text with emoji ♥.
Convert Golang String to Rune
In Golang, we often use strings to store character data. But rune slices have many advantages: they treat characters more consistently. For Unicode characters, a rune slice can be used to append and modify characters with no errors. If we act on a string directly, we can cause problems here.
See the following code.
package main import ( "fmt" ) func main() { r := []rune("CoronaVirus") fmt.Println(r) fmt.Printf("%U\n", r) }
Output
go run hello.go [67 111 114 111 110 97 86 105 114 117 115] [U+0043 U+006F U+0072 U+006F U+006E U+0061 U+0056 U+0069 U+0072 U+0075 U+0073]
In the above code, Converting the string to a slice of runes yields a slice whose elements are the Unicode code points of the string.
Rune slices are also ideal for acquiring substrings from a string. This supports Unicode characters with no risk of data corruption.
Convert Golang runes to string
When you convert rune to string, you get the new string that is the concatenation of the runes converted to UTF-8 encoded strings. Values outside a range of valid Unicode code points are converted to \uFFFD, a Unicode replacement character.
See the following code.
package main import ( "fmt" ) func main() { s := string([]rune{'\u0051', '\u0052', '\u0053', '\u20BC'}) fmt.Println(s) }
Output
go run hello.go QRS₼
Performance
Converting a string to rune create a new slice and convert rune to a string creates a new string and therefore have time complexity proportional to the number of bytes that are processed.
Conclusion
They are the same thing in 3 different formats.
- String is an immutable byte sequence.
- Byte slice is a mutable byte sequence.
- Rune slice is re-grouping of byte slice so that each index is a character.
The string is an excellent way to deal with short sequences of bytes or characters. Every time you operate on a string, such as a find replace string or take substring, a new string is created. This is very inefficient if the string is huge, such as file content.
Converting a string to a rune slice in Golang is a standard operation. It guides the code that will be less likely to corrupt Unicode characters.
Finally, Convert String To Rune in Go Example is over.