As stated in @Michael's first-rate comment fmt.Println([]rune("foo"))
is a conversion of a string to a slice of runes []rune. When you convert from string to []rune, each utf-8 char in that string becomes a Rune. See https://mcmap.net/q/56928/-what-is-a-rune. Similarly, in the reverse conversion, when converted from []rune to string, each rune becomes a utf-8 char in the string. See https://mcmap.net/q/56928/-what-is-a-rune. A []rune can also be set to a byte, float64, int or a bool.
package main
import (
. "fmt"
)
func main() {
r := []rune("foo")
c := []interface{}{byte(r[0]), float64(r[0]), int(r[0]), r, string(r), r[0] != 0}
checkType(c)
}
func checkType(s []interface{}) {
for k, _ := range s {
Printf("%T %v\n", s[k], s[k])
}
}
byte(r[0])
is set to “uint8 102”, float64(r[0])
is set to “float64 102”,int(r[0])
is set to “int 102”, r
is the rune” []int32 [102 111 111]”, string(r)
prints “string foo”, r[0] != 0
and shows “bool true”.
[]rune to string conversion is supported natively by the spec. See the comment in https://mcmap.net/q/56929/-golang-converting-from-rune-to-string. In Go then a string is a sequence of bytes. However, since multiple bytes can represent a rune code-point, a string value can also contain runes. So, it can be converted to a []rune , or vice versa. See https://mcmap.net/q/56928/-what-is-a-rune.
Note, there are only two built-in type aliases in Go, byte (alias of uint8) and rune (alias of int32). See https://Go101.org/article/type-system-overview.html. Rune literals are just 32-bit integer values. For example, the rune literal 'a' is actually the number "97". See https://mcmap.net/q/56928/-what-is-a-rune. Quotes edited.
rune
is a type, not a function. – Hennahanestring(r)
to set a rune to a string. See https://mcmap.net/q/56310/-golang-how-does-the-rune-function-work.[]rune("abc")
to set a string to a rune. See https://mcmap.net/q/56310/-golang-how-does-the-rune-function-work. – Purposeful