I have a word which contains some of these characters - šđžčć. When I take the first letter out of that word, I'll have a byte
, when I convert that byte
into string I'll get incorrectly decoded string. Can someone help me figure out how to decode properly the extracter letter. This is example code:
package main
import (
"fmt"
)
func main() {
word := "ŠKOLA"
c := word[0]
fmt.Println(word, string(c)) // ŠKOLA Å
}
Š
is more than one byte. One method to index runes is to convert the string to []rune
c := []rune(word)[0]
https://play.golang.org/p/NBUopxe-ik1
You can also use the functions provided in the utf8
package, like utf8.DecodeRune
and utf8.DecodeRuneInString
to iterate over the individual codepoints in the utf8 string.
r, _ := utf8.DecodeRuneInString(word)
fmt.Println(word, string(r))