3

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"))
Joff
  • 11,247
  • 16
  • 60
  • 103
  • 4
    `rune` is a type, not a function. – JimB Aug 24 '16 at 10:20
  • 1
    That code is trying to do a [conversion](https://golang.org/ref/spec#Conversions) 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. – Michael Aug 24 '16 at 10:23
  • Think char while you're getting familiar with rune – Sridhar Aug 24 '16 at 10:36
  • rune is Go terminology for a single Unicode code point. See https://golang.org/doc/effective_go.html#for. `string(r)` to set a rune to a string. See https://stackoverflow.com/a/62739051/12817546. `[]rune("abc") ` to set a string to a rune. See https://stackoverflow.com/a/62739051/12817546. –  Jul 10 '20 at 00:08

2 Answers2

8

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

mibollma
  • 14,959
  • 6
  • 52
  • 69
jussius
  • 3,114
  • 15
  • 21
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://stackoverflow.com/a/51611567/12817546. Similarly, in the reverse conversion, when converted from []rune to string, each rune becomes a utf-8 char in the string. See https://stackoverflow.com/a/51611567/12817546. 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://stackoverflow.com/a/46021588/12817546. 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://stackoverflow.com/a/19325804/12817546.

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://stackoverflow.com/a/19311218/12817546. Quotes edited.

kapad
  • 611
  • 2
  • 11
  • 27
  • A boolean, numeric, or string type can be set to another type. For`[]byte` see https://stackoverflow.com/a/62725637/12817546, for `float64` see https://stackoverflow.com/a/62753031/12817546, `int` see https://stackoverflow.com/a/62737936/12817546, `string` see https://stackoverflow.com/a/62740786/12817546 and for `bool` see https://stackoverflow.com/a/62726854/12817546. –  Jul 07 '20 at 09:28