Consider the following example, we are iterating over a string
import (
"fmt"
"reflect"
)
func main() {
str := "hello world"
for i := 0; i < len(str); i++ {
element := str[i]
fmt.Print(element, " ")
fmt.Println(reflect.TypeOf(element))
}
}
104 uint8
The value str[i]
actually return a byte of uint8
, or a rune
To convert a byte to a string
string(var)
To convert a byte array to string
string(byteArray)