golang how does the rune() function work
Asked Answered
I

2

4

I came across a function posted online that used the rune() function in golang, but I am having a hard time looking up what it is. I am going through the tutorial and inexperienced with the docs so it is hard to find what I am looking for.

Specifically, I am trying to see why this fails...

fmt.Println(rune("foo"))

and this does not

fmt.Println([]rune("foo"))
Inebriant answered 24/8, 2016 at 10:16 Comment(4)
rune is a type, not a function.Hennahane
That code is trying to do a conversion from type string to type rune and []rune. A rune is a unicode code point. You cannot convert a string to a single rune, but you can convert it to a slice of runes as in the second example.Orientalism
Think char while you're getting familiar with runeMoccasin
rune is Go terminology for a single Unicode code point. See golang.org/doc/effective_go.html#for. string(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
N
9

rune is a type in Go. It's just an alias for int32, but it's usually used to represent Unicode points. rune() isn't a function, it's syntax for type conversion into rune. Conversions in Go always have the syntax type() which might make them look like functions.

The first bit of code fails because conversion of strings to numeric types isn't defined in Go. However conversion of strings to slices of runes/int32s is defined like this in language specification:

Converting a value of a string type to a slice of runes type yields a slice containing the individual Unicode code points of the string. [golang.org]

So your example prints a slice of runes with values 102, 111 and 111

Neurasthenia answered 24/8, 2016 at 10:45 Comment(1)
can you please edit your post to include the link where you found that info?Inebriant
P
0

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.

Purposeful answered 5/7, 2020 at 9:36 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.