Introduction
In Go, the rune
type is used to represent a Unicode code point. A rune
is essentially an alias for int32
and allows you to work with Unicode characters, making it useful for handling multilingual text and special symbols. In this chapter, you will learn the basics of the rune
type, including how to work with runes in strings and how to perform common operations on them.
Rune Basics
A rune
represents a single Unicode code point and can store any Unicode character.
Declaring a Rune
You can declare a rune
variable using single quotes ('
) to represent a character.
Example:
package main
import "fmt"
func main() {
var r rune = 'a'
fmt.Printf("Rune: %c, Unicode: %U, Code point: %d\n", r, r, r)
// Output: Rune: a, Unicode: U+0061, Code point: 97
}
Working with Runes in Strings
Strings in Go are sequences of bytes, but they can also be interpreted as sequences of runes. You can convert a string to a slice of runes to work with individual characters.
Example: Iterating Over a String as Runes
Example:
package main
import "fmt"
func main() {
str := "Hello, ??"
runes := []rune(str)
for i, r := range runes {
fmt.Printf("Index: %d, Rune: %c, Unicode: %U, Code point: %d\n", i, r, r, r)
}
}
Example: Modifying a String Using Runes
Example:
package main
import "fmt"
func main() {
str := "Hello, ??"
runes := []rune(str)
// Modify the first rune
runes[0] = 'h'
newStr := string(runes)
fmt.Println(newStr) // Output: hello, ??
}
Common Rune Operations
Checking if a Rune is a Letter or Digit
The unicode
package provides functions to check if a rune is a letter, digit, or other types.
Example:
package main
import (
"fmt"
"unicode"
)
func main() {
r := '9'
if unicode.IsLetter(r) {
fmt.Printf("%c is a letter\n", r)
} else if unicode.IsDigit(r) {
fmt.Printf("%c is a digit\n", r)
} else {
fmt.Printf("%c is neither a letter nor a digit\n", r)
}
}
Converting Runes to Uppercase or Lowercase
The unicode
package also provides functions to convert runes to uppercase or lowercase.
Example:
package main
import (
"fmt"
"unicode"
)
func main() {
r := 'a'
upperR := unicode.ToUpper(r)
fmt.Printf("Original: %c, Uppercase: %c\n", r, upperR) // Output: Original: a, Uppercase: A
r = 'A'
lowerR := unicode.ToLower(r)
fmt.Printf("Original: %c, Lowercase: %c\n", r, lowerR) // Output: Original: A, Lowercase: a
}
Checking Rune Properties
You can use the unicode
package to check various properties of runes, such as if they are letters, digits, spaces, etc.
Example:
package main
import (
"fmt"
"unicode"
)
func main() {
runes := []rune{'a', '1', ' ', '?'}
for _, r := range runes {
fmt.Printf("Rune: %c\n", r)
if unicode.IsLetter(r) {
fmt.Println(" Is a letter")
}
if unicode.IsDigit(r) {
fmt.Println(" Is a digit")
}
if unicode.IsSpace(r) {
fmt.Println(" Is a space")
}
if unicode.Is(unicode.Han, r) {
fmt.Println(" Is a Han character")
}
}
}
Converting Between Strings and Runes
You can convert between strings and slices of runes using type conversion.
Example: Converting String to Runes and Back
Example:
package main
import "fmt"
func main() {
str := "Hello, ??"
runes := []rune(str)
fmt.Println("String:", str)
fmt.Println("Runes:", runes)
newStr := string(runes)
fmt.Println("New String:", newStr)
}
Conclusion
The rune
type in Go is essential for working with Unicode characters and strings. By understanding how to declare, manipulate, and perform operations on runes, you can handle multilingual text and special symbols effectively. The unicode
package provides many useful functions for checking and transforming runes, making it used for text processing in Go.