Golang: Rune

By Xah Lee. Date: . Last updated: .

“Rune” means Unicode codepoint. (think of it as a character.) It is a term golang invented.

When you see the word “rune”, you can think of it as any or all of the following:

[see Golang: Basic Types]

The purpose of rune is similar to character type in some other languages.

Unicode gives each character a integer id. This integer id is called codepoint.

Note, not all codepoint are character as we think of. For example, newline, tab, non-breaking-space, right-to-left mark, combining character accents, are all codepoints.

Unicode has a standard notation for codepoint, starting with U+ followed by its codepoint in hexadecimal. E.g.

Rune Literal

A “rune literal” is a syntax to represent one Unicode character. Like this:

'c' → rune literal. Where the c is 1 single Unicode character. (but may be presented by escape sequence)

All of the following are rune values.

to see all backslash escapes, see Golang: String

Print a rune in decimal, hex, and standard unicode notations

package main

import "fmt"

func main() {

	// print a rune in decimal, hex, and standard unicode notations

	var capA = 'A'

	fmt.Printf("%d %x %U\n", capA, capA, capA) // 65 41 U+0041
}

Print Rune

Rune is codepoint, thus is a integer.

The following format works with integer:

package main

import "fmt"

// print rune in different formats

func main() {
    var x = '😂'

    fmt.Printf("%c\n", x) // 😂
    fmt.Printf("%q\n", x) // '😂'
    fmt.Printf("%U\n", x) // U+1F602

    fmt.Printf("%b\n", x) // 11111011000000010
    fmt.Printf("%o\n", x) // 373002
    fmt.Printf("%d\n", x) // 128514
    fmt.Printf("%x\n", x) // 1f602
}

Reference

The Go Programming Language Specification - The Go Programming Language#String_literals

If you have a question, put $5 at patreon and message me.

Golang

  1. Compile, Run
  2. Source Encoding
  3. Package, Import
  4. Comment
  5. Print
  6. String
  7. Rune
  8. String as Chars
  9. Variable
  10. Zero Value
  11. Constant
  12. If Then Else
  13. Switch/Case
  14. Loop
  15. Basic Types
  16. Array
  17. Slice
  18. Map
  19. Struct
  20. Function
  21. regexp
  22. Read File
  23. Write to File
  24. Walk Dir
  25. Check File Exist
  26. System Call
  27. Get Script Path
  28. Pointer
  29. Defer
  30. Random Number

Examples

  1. match any regexp
  2. Validate Links
  3. Generate Sitemap

Reference

  1. Go Spec