5

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"))
4
  • 4
    rune is a type, not a function. Commented Aug 24, 2016 at 10:20
  • 2
    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. Commented Aug 24, 2016 at 10:23
  • Think char while you're getting familiar with rune Commented Aug 24, 2016 at 10:36
  • 1
    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 stackoverflow.com/a/62739051/12817546. []rune("abc") to set a string to a rune. See stackoverflow.com/a/62739051/12817546. Commented Jul 10, 2020 at 0:08

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

1 Comment

can you please edit your post to include the link where you found that info?
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.

1 Comment

A boolean, numeric, or string type can be set to another type. For[]byte see stackoverflow.com/a/62725637/12817546, for float64 see stackoverflow.com/a/62753031/12817546, int see stackoverflow.com/a/62737936/12817546, string see stackoverflow.com/a/62740786/12817546 and for bool see stackoverflow.com/a/62726854/12817546.
0

Think of runes as an abstraction layer built on purpose into the language so it can transparently support UTF-8. Since not everybody speaks English, and you might find yourself handling other languages like French, German or Japanese, I would advise you to take a good read on this: https://go.dev/blog/strings

And also to try running this:

import "fmt"

func main() {
    fmt.Println([]rune("Hello 世界!"))
}

You will see that the Sino-Japanese characters have a peculiar numerical difference when printing their values in comparison with the other chars:

[72 101 108 108 111 32 19990 30028 33]

This is because these are not on the ASCII table and you can't represent them with a single byte. When Golang was created it was designed with Unicode support in mind and part of the language. You don't need a library for handling UTF-8. It's built-in, like written in stone!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.