Iterate over UTF-8 or non-ASCII strings in Golang. Iterations by for or range
keyword.
for loop iteration
Run Code on Go Playground
package main
import "unicode/utf8"
import "fmt"
func main() {
str := "I am 字串"
for i, w := 0, 0; i < len(str); i += w {
runeValue, width := utf8.DecodeRuneInString(str[i:])
w = width
fmt.Println(string(runeValue))
}
}
Run Code on Go Playground
package main
import "fmt"
func main() {
str := "I am 字串"
for index, runeValue := range str {
fmt.Println(index, string(runeValue))
}
}
Tested on: Ubuntu Linux 15.10, Go 1.5.3.
References: