[Golang] Iterate Over UTF-8 Strings (non-ASCII strings)


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))
        }
}

range iteration

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:

[1]go iterate over string
[2]Strings, bytes, runes and characters in Go - The Go Blog
[3]utf8 - The Go Programming Language
[4]support non-ascii alphabet (utf8 alphabet) · siongui/go-succinct-data-structure-trie@8ad7e26 · GitHub
[5]I need help understanding runes : golang