What is a rune in Go and how is it different from byte?
Hi everyone! I just started learning Go and came across a strange data type — rune. I read the documentation but still don't fully understand why it's needed.
I'm trying to work with strings and iterate over them. I wrote this code:
```go
package main
import "fmt"
func main() {
s := "Привет, мир!"
for i := 0; i < len(s); i++ {
fmt.Printf("%c ", s[i])
}
}
```
But instead of the expected characters, I got some gibberish: `П Ð Ð¸ в е Ñ , м и Ñ !`. I expected to see each character of the string in order, but the output clearly doesn't match what I wanted. Why is this happening?
Then I replaced the loop with `for i, r := range s`, and everything worked correctly. The documentation says that `r` has type `rune`.
Please explain:
1. What is a `rune` in simple terms?
2. What is the difference
No answers yet.
Log in to leave an answer