Go言語で文字列を1文字ずつ処理する
rangeにstringを渡すとbyte positionとruneを1つずつ取得できる
package main import ( "fmt" "unicode/utf8" ) func main() { s := "あいうえお" // rangeにstringを渡すとpos,runeを順に返す for pos, r := range s { fmt.Printf("character %c starts at byte position %d. unicode format is %U\n", r, pos, r) } // -> character あ starts at byte position 0. unicode format is U+3042 'あ' // -> character い starts at byte position 3. unicode format is U+3044 'い' // -> character う starts at byte position 6. unicode format is U+3046 'う' // -> character え starts at byte position 9. unicode format is U+3048 'え' // -> character お starts at byte position 12. unicode format is U+304A 'お' }