Strings and Runes in Go
Go: Strings and Runes
In Go, strings are essentially sequences of bytes. However, sometimes we need to work with individual Unicode characters in a string, rather than just individual bytes. This is where the concept of "string runes" comes in.
A rune is a Unicode code point, which is an integer value that represents a single character in a string. In Go, a rune is represented using the rune data type, which is an alias for the int32 data type.
How to work with string runes in Go
To work with string runes in Go, we can use the for range loop, which iterates over the Unicode code points of a string. For example:
package main
import "fmt"
func main() {
str := "Hello, 世界"
for _, r := range str {
fmt.Printf("%c", r)
}
}
Explainations: we define a string str
that contains the text "Hello, 世界". We then use the for range loop to iterate over the Unicode code points of the string. The loop assigns each code point to the variable r, which is of type rune. We can then print out each code point using the %c format specifier, which tells fmt.Printf() to print the code point as a Unicode character.
The output of this program would be:
Hello, 世界
Note that in the for range loop, we use the _
identifier to discard the index of each code point, since we don't need it in this case. If we did need the index, we could use a regular for
loop instead and use the len()
and []rune
functions to access individual runes in the string.
Previous Article
Next Article
Golang Tutorials
- Hello World
- Operators in Go
- Declarations in Go
- Values in Go
- Variables in Go
- For in Go
- If/Else in Go
- Switch in Go
- Arrays in Go
- Slices in Go
- Maps in Go
- Range in Go
- Functions in Go
- Closures in Go
- Recursion in Go
- Pointers in Go
- Strings and Runes in Go
- Structs in Go
- Methods in Go
- Interfaces in Go
- Generics in Go
- Errors in Go
- Goroutines in Go
- Channels in Go
- Select in Go
- Timeouts in Go
- Timers in Go
- Worker Pools in Go
- WaitGroups in Go
- Mutexes in Go
- Sorting in Go
- Panic in Go
- Defer in Go
- Recover in Go
- JSON in Go
- XML in Go
- Time in Go
- Epoch in Go
- Time Formatting in Go
- Random Numbers in Go
- Number Parsing in Go
- URL Parsing in Go
- SHA256 Hashes in Go
- Base64 Encoding in Go
- Reading Files in Go
- Writing Files in Go
- File Paths in Go
- Directories in Go
- Testing and Benchmarking in Go
- Command-Line Arguments in Go
- Command-Line Flags in Go
- Command-Line Subcommands in Go
- Environment Variables in Go
- HTTP Client in Go
- HTTP Server in Go
- Context in Go
- Signals in Go