Encoding method
In Golang, Runes are used to represent Unicode characters. Each Rune is a 32-bit integer representing a single Unicode code point. The encoding method in Golang for Runes ensures that characters from various languages and scripts can be efficiently handled within programs. Golang provides built-in functions and methods to work with Runes, enabling developers to manipulate and process Unicode text effectively.
Declaration
In Golang, Runes are declared using the rune keyword. For example, var r rune = 'a'. This declares a variable r of type rune, initialized with the Unicode code point for the lowercase letter 'a'. By using the rune type, Golang ensures proper handling of Unicode characters, allowing developers to work with text in a flexible and language-independent manner.
Character vs. rune
In Golang, a "character" typically refers to a single UTF-8 encoded byte, which may represent a part of a Unicode character or an ASCII character. On the other hand, a "rune" refers to a Unicode code point, represented by a 32-bit integer. While a character in Golang can be of variable length due to UTF-8 encoding, a rune always represents a single Unicode code point, providing a more consistent and reliable way to work with Unicode text in Golang programs.
Important Points to Consider
- Bytes make up strings, which can include valid characters that can be represented with runes.
- To convert a string to an array of runes, we use the rune() method.
- Always keep in mind that a string is a collection of bytes, not a rune. However, a string could contain Unicode content encoded in UTF-8, and because the go source code always encodes in UTF-8, there is no need to encode the string in UTF-8.
- UTF-8 encodes all Unicode characters in 1 to 4 bytes, with 1 byte for ASCII and the remainder for the rune.
- ASCII has a total of 256 characters. In which 128 is the number of characters and 0-127 is the number of code points. The element that represents a single value is referred to as a code point.
package main
import "fmt"
func main() {
chr := 'z'
chrRune := rune(chr)
fmt.Println("The rune")
fmt.Println(chrRune)
}
Output:
The rune
122
Now let us see how to convert string into array of runes.
package main
import "fmt"
func main() {
strg := "ThisIsAString"
strRuneArray := []rune(strg)
fmt.Println("The rune array")
fmt.Println(strRuneArray)
}
Output:
The rune array
[84 104 105 115 73 115 65 83 116 114 105 110 103]
Here is another example.
package main
import (
"fmt"
"reflect"
)
func main() {
runeOne := 'd'
runeTwo := 'q'
fmt.Printf("Unicode: %U; Type: %s", runeOne, reflect.TypeOf(runeOne))
fmt.Printf("\nUnicode: %U; Type: %s", runeTwo, reflect.TypeOf(runeTwo))
}
Output:
Unicode: U+0064; Type: int32
Unicode: U+0071; Type: int32
Frequently Asked Questions
What is rune in programming?
Rune represents a Unicode code point in programming, typically a 32-bit integer, used for handling characters and text.
What is the difference between string and rune in Go?
Strings are a sequence of bytes, while runes represent Unicode code points, ensuring proper handling of characters.
What is the rune code point in Golang?
Rune code points in Golang are 32-bit integers representing Unicode characters, facilitating language-independent text processing.
Conclusion
In this article, we have extensively discussed runes and their implementation in the Golang programming language. We hope that this blog has helped you enhance your knowledge regarding runes and if you would like to learn more, check out our articles on GoLang. Do upvote our blog to help other ninjas grow.
Happy Coding!