4 ms·
1. Range on a string iterates over Unicode code points (runes): s := "Какая-то строка" for _, rune := range s { // do something with rune 'К',
by dchest 16y ago
1. Range on a string iterates over Unicode code points (runes):
s := "Какая-то строка"
for _, rune := range s {
// do something with rune 'К', 'a', ...
}
2. Converting to []int gives you a slice of runes:
s := "Какая-то строка"
runes := []int(s)
sub := string(runes[:8]) // "Какая-то"
however, slicing a string directly will slice it by byte:
s[:8] // "Кака"
3. With package utf8 (http://golang.org/pkg/utf8/ http://golang.org/pkg/utf8/) you can manipulate runes manually.
While this is all not intuitive (you have to know what does what), I find it rather easy.