What is a rune?

go, rune, terminology

Solution

Rune literals are just 32-bit integer values (however they're untyped constants, so their type can change). They represent unicode codepoints. For example, the rune literal `'a'` is actually the number `97`.

Therefore your program is pretty much equivalent to:

package main

import "fmt"

func SwapRune(r rune) rune {
    switch {
    case 97 <= r && r <= 122:
        return r - 32
    case 65 <= r && r <= 90:
        return r + 32
    default:
        return r
    }
}

func main() {
    fmt.Println(SwapRune('a'))
}

It should be obvious, if you were to look at the Unicode mapping, which is identical to ASCII in that range. Furthermore, 32 is in fact the offset between the uppercase and lowercase codepoint of the character. So by adding `32` to `'A'`, you get `'a'` and vice versa.

Problem

What is a `rune` in Go? I've been googling but Golang only says in one line: `rune` is an alias for `int32`. But how come integers are used all around like swapping cases? The following is a function swapcase. What is all the `<=` and `-`? And why doesn't `switch` have any arguments? `&&` should mean and but what is `r <= 'z'`? ``` func SwapRune(r rune) rune { switch { case 'a' <= r && r <= 'z': return r - 'a' + 'A' case 'A' <= r && r <= 'Z': return r - 'A' + 'a' default: return r } } ``` Most of them are from http://play.golang.org/p/H6wjLZj6lW ``` func SwapCase(str string) string { return strings.Map(SwapRune, str) } ``` I understand this is mapping `rune` to `string` so that it can return the swapped string. But I do not understand how exactly `rune` or `byte` works here.

Original source

Related problems