Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

for文におけるrange節について

日本語や中国語も含めたマルチバイト文字もよろしくパースしてくれます
UTF-8でサポートしてる必要があるので、ヒエログリフとかは駄目です。

package main

import "fmt"

func main() {
	for pos, char := range "これ不错one까요?" {
		fmt.Printf("character %c starts at byte position %d\n", char, pos)
	}
}
$ go run forsample.go 
character こ starts at byte position 0
character れ starts at byte position 3
character 不 starts at byte position 6
character 错 starts at byte position 9
character o starts at byte position 12
character n starts at byte position 13
character e starts at byte position 14
character 까 starts at byte position 15
character 요 starts at byte position 18
character ? starts at byte position 21