In the Go programming language, a rune is a basic data type designed to store a Unicode code point.
Questions tagged [rune]
58 questions
320
votes
10 answers
What is a rune?
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…
user2671513
116
votes
4 answers
How can I iterate over a string by runes in Go?
I wanted to this:
for i := 0; i < len(str); i++ {
dosomethingwithrune(str[i]) // takes a rune
}
But it turns out that str[i] has type byte (uint8) rather than rune.
How can I iterate over the string by runes rather than bytes?

Matt
- 21,026
- 18
- 63
- 115
69
votes
5 answers
Golang converting from rune to string
I have the following code, it is supposed to cast a rune into a string and print it. However, I am getting undefined characters when it is printed. I am unable to figure out where the bug is:
package main
import (
"fmt"
"strconv"
…

user3551708
- 843
- 1
- 7
- 14
53
votes
4 answers
Convert rune to int?
In the following code, I iterate over a string rune by rune, but I'll actually need an int to perform some checksum calculation. Do I really need to encode the rune into a []byte, then convert it to a string and then use Atoi to get an int out of…

miku
- 181,842
- 47
- 306
- 310
25
votes
5 answers
Go sort a slice of runes?
I'm having trouble sorting strings by character (to check whether two strings are anagrams, I want to sort both of them, and check for equality).
I can get a []rune representation of the string s like this:
runes := make([]rune, len(s))
copy(runes,…

andras
- 6,339
- 6
- 26
- 22
22
votes
3 answers
Rune vs byte ranging over string
According to https://blog.golang.org/strings and my testings, it looks like while we range a string, the characters we get are rune type, but if we get it by str[index], they will be byte type, why is it?

user8142520
- 751
- 1
- 6
- 20
17
votes
2 answers
What are Go's rules for comparing bytes with runes?
I've discovered the following peculiarity:
b := "a"[0]
r := 'a'
fmt.Println(b == r) // Does not compile, cannot compare byte and rune
fmt.Println("a"[0] == 'a') // Compiles and prints "true"
How does this work?

EMBLEM
- 2,207
- 4
- 24
- 32
16
votes
2 answers
Slice unicode/ascii strings in golang?
I need to slice a string in Go. Possible values can contain Latin chars and/or Arabic/Chinese chars. In the following example, the slice annotation [:1] for the Arabic string alphabet is returning a non-expected value/character.
package main
…

Jonathan Simon Prates
- 1,122
- 2
- 12
- 28
14
votes
4 answers
How to get a substring from a string of runes in golang?
I found this, https://groups.google.com/forum/#!topic/golang-nuts/YyKlLwuWt3w but as far as I can tell, the solutions didn't work for me.
If you use the method of treating a string as a slice(str[:20]), it breaks off in the middle of characters and…

John
- 3,037
- 8
- 36
- 68
10
votes
2 answers
Position in characters of a substring in Go
How can I know the position of a substring in a string, in characteres (or runes) instead of bytes?
strings.Index(s, sub) will give the position in bytes. When using Unicode, it doesn't match the position in runes:…

siritinga
- 4,063
- 25
- 38
10
votes
2 answers
Cast/convert int to rune in Go
Assuming I have an int64 variable (or other integer size) representing a valid unicode code-point, and I want to convert it into a rune in Go, what do I do?
In C I would have used a type cast something like:
c = (char) i; // 7 bit ascii only
But…

Roboprog
- 3,054
- 2
- 28
- 27
9
votes
3 answers
How to return a blank rune
I'm looking at the string.Map function which must take a mapping function which returns a rune. I would like to eliminate runes that resolves false with a call to: unicode.IsPrint()
func Map(mapping func(rune) rune, s string) string
My function…

user398520
- 1,553
- 3
- 12
- 11
8
votes
2 answers
How to convert a string to rune?
Here is my code snippet:
var converter = map[rune]rune {//some data}
sample := "⌘こんにちは"
var tmp string
for _, runeValue := range sample {
fmt.Printf("%+q", runeValue)
tmp = fmt.Sprintf("%+q", runeValue)
}
The output of…

Amid
- 442
- 2
- 8
- 15
6
votes
1 answer
How to decode a string containing backslash-encoded Unicode characters?
I have a string stored as a:
a := `M\u00fcnchen`
fmt.Println(a) // prints "M\u00fcnchen"
b := "M\u00fcnchen"
fmt.Println(b) // prints "München"
Is there a way I can convert a into b ?

Daniele B
- 19,801
- 29
- 115
- 173
5
votes
1 answer
cannot use type []rune as type rune in append
package main
var lettersLower = []rune("abcdefghijklmnopqrstuvwxyz")
var lettersUpper = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
func main() {
x := append(lettersLower, lettersUpper)
}
Why does this not work? How can I append lettersLower and…

fnkr
- 9,428
- 6
- 54
- 61