I have encountered a situation that I do not understand.
a := "hello"
fmt.Printf("%v %T\n",a[0],a[0])
This gives 104 uint8
.
for _,v := range a {
fmt.Printf("%v %T\n",v,v)
}
This gives 104 int32
for the first iteration. I don't understand why their types are not same. First one is byte
, second one is rune
. I expect both to be byte
.
This might explain it:
https://blog.golang.org/strings
In short: if
a
is a string,a[i]
is a byte, but here,r
is arune
:When you range over a string, you range over the runes of that string. To range over the bytes of a string, use: