Example of looping over runes in a string in Go

This example program shows how to loop over the runes in a Go string.

package main

import (
        "fmt"
)

func main() {
        kanji := "南無妙法蓮華経"
        for pos, char := range kanji {
                fmt.Printf("character %c [%X] starts at byte %d\n",
                        char, char, pos)
        }
}

(download)

The output looks like this:

character 南 [5357] starts at byte 0
character 無 [7121] starts at byte 3
character 妙 [5999] starts at byte 6
character 法 [6CD5] starts at byte 9
character 蓮 [84EE] starts at byte 12
character 華 [83EF] starts at byte 15
character 経 [7D4C] starts at byte 18


Copyright © Ben Bullock 2009-2018. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer