私はこれをしたかったのです:
for i := 0; i < len(str); i++ {
dosomethingwithrune(str[i]) // takes a rune
}
str[i]
しかし、の型は ではなくbyte
( ) であることがわかりました。uint8
rune
バイトではなくルーンで文字列を反復処理するにはどうすればよいですか?
ベストアンサー1
この例をご覧ください効果的な囲碁:
for pos, char := range "日本語" {
fmt.Printf("character %c starts at byte position %d\n", char, pos)
}
次のように出力されます:
character 日 starts at byte position 0
character 本 starts at byte position 3
character 語 starts at byte position 6
文字列の場合、範囲はより多くの作業を実行し、UTF-8 を解析して個々の Unicode コード ポイントを分割します。