package main
import (
"fmt"
)
func main() {
s := "str"
for i := 0; i < len(s); i++ {
c := s[i]
fmt.Printf("Byte at index %d is '%c' (0x%x)\n", i, c, c)
}
}
Byte at index 0 is 's' (0x73)
Byte at index 1 is 't' (0x74)
Byte at index 2 is 'r' (0x72)
package main
import (
"fmt"
)
func main() {
s := "中国汉字"
for i, runeChar := range s {
fmt.Printf("Rune at byte position %d is %#U\n", i, runeChar)
}
}
Rune at byte position 0 is U+4E2D '中'
Rune at byte position 3 is U+56FD '国'
Rune at byte position 6 is U+6C49 '汉'
Rune at byte position 9 is U+5B57 '字'