In the Go programming language, a rune is a basic data type designed to store a Unicode code point.
Questions tagged [rune]
58 questions
4
votes
1 answer
Which is better to get the last X character of a Golang String?
When I have string "hogemogehogemogehogemoge世界世界世界" which code is better to get last rune with avoiding memory allocation?
There are similar question about to get last X character of Golang String.
How to get the last X Characters of a Golang…

guitarrapc
- 534
- 5
- 9
3
votes
3 answers
Get the width of Chinese strings correctly
I want to make a border around the text 这是一个测试, but I cannot get the actual width of it. With English text, it does work perfectly.
Here is my analysis:
len tells me this:
这是一个测试 18
aaaaaaaaa 10
つのだ☆HIRO 16
aaaaaaaaaa 10
runewidth.StringWidth…

MarvinJWendt
- 2,357
- 1
- 10
- 36
3
votes
2 answers
Does the conversion from string to rune slice make a copy?
I'm teaching myself Go from a C background.
The code below works as I expect (the first two Printf() will access bytes, the last two Printf() will access codepoints).
What I am not clear is if this involves any copying of data.
package main
import…

Remo.D
- 16,122
- 6
- 43
- 74
3
votes
1 answer
Go rune literal for high positioned emojis
How do we use an emoji with a rune literal that is beyond I think
code point U+265F?
a1 := '\u2665'
this works
a2 := '\u1F3A8'
this gives error invalid character literal, more that one character.
Is there a way to represent higher positioned…

Jan Bodnar
- 10,969
- 6
- 68
- 77
3
votes
2 answers
How capacity of []rune is determined when converting from a string
Can someone explain why I got different capacity when converting the same string in []rune?
Take a look at this code
package main
import (
"fmt"
)
func main() {
input := "你好"
runes := []rune(input)
fmt.Printf("len…

GeorgesTimoun
- 85
- 1
3
votes
2 answers
Is there any difference between range over string and range over rune slice?
Ranging over string
func main() {
str := "123456"
for _, s := range str {
fmt.Printf("type of v: %s, value: %v, string v: %s \n", reflect.TypeOf(s), s, string(s))
}
}
https://play.golang.org/p/I1JCUJnN41h
And ranging over rune…

Shaonv
- 43
- 8
3
votes
1 answer
Is there an equivalent to the wcwidth() function in Go?
The POSIX function wcwidth() computes the width of a given wchar_t when printed on a terminal. For instance, wcwidth(L'A') returns 1, wcwidth(L'字') returns 2, etc. There is also a function wcswidth() which computes the width of an entire string—this…

fuz
- 88,405
- 25
- 200
- 352
2
votes
1 answer
Difference between length of rune slice and RuneCountInString?
We can get the number of runes in a string by getting the length of the rune slice converted from the string.
s := "世界"
runes := []rune(s)
fmt.Println(len(runes))
Or use the RuneCountInString function in unicode/utf8…

Wang Ke
- 329
- 3
- 11
2
votes
1 answer
Access random rune element of string without using for ... range
I recently asked this question and the answers increased my understanding, but they didn't solve the actual problem I had. So, I will try to ask a similar but different question as follows.
Suppose that I want to access random rune element of a…

Eissa N.
- 1,695
- 11
- 18
2
votes
2 answers
Does accessing elements of string as byte perform conversion?
In Go, to access elements of a string, we can write:
str := "text"
for i, c := range str {
// str[i] is of type byte
// c is of type rune
}
When accessing str[i] does Go perform a conversion from rune to byte? I would guess the answer is yes,…

Eissa N.
- 1,695
- 11
- 18
2
votes
1 answer
How do I distinguish a rune and int32 values in a typeswitch?
Having the following code
var v interface{}
v = rune(1)
switch v.(type) {
case int32:
fmt.Println("int32")
case rune:
fmt.Println("rune")
}
I get a compilation error
tmp/sandbox193184648/main.go:14: duplicate case rune in…

user7610
- 25,267
- 15
- 124
- 150
2
votes
2 answers
Can't use unicode character as rune
It appears that golang doesn't support all unicode characters for its runes
package main
import "fmt"
func main() {
standardSuits := []rune{'♠️', '♣️', '♥️', '♦️'}
fmt.Println(standardSuits)
}
Generates the following error:
./main.go:6:…

Electric Coffee
- 11,733
- 9
- 70
- 131
2
votes
2 answers
Get unicode category from rune
I'm looking for a way to get the unicode category (RangeTable) from a rune in Go. For example, the character a maps to the Ll category. The unicode package specifies all of the categories (http://golang.org/pkg/unicode/#pkg-variables), but I don't…

Tyler Treat
- 14,640
- 15
- 80
- 115
1
vote
2 answers
How can I scan a rune?
So far, I haven't been able to print a rune by scanning it with fmt.Scan and printing it with fmt.Print. This is the vary basic code I'm working on:
package main
import "fmt"
func main() {
var c rune
fmt.Scan(&c)
fmt.Printf("%c",…

Dodeiro
- 35
- 2
1
vote
1 answer
To make Golang rune to utf-8 result same as js string.fromCharCode
go
var int32s = []int32{
8, 253, 80, 56, 30, 220, 217, 42, 235, 33, 211, 23, 231, 216, 234, 26,
}
fmt.Println("word: ", string(int32s))
js
let int32s = [8, 253, 80, 56, 30, 220, 217, 42, 235, 33, 211, 23, 231, 216, 234, 26]
str =…

Tpircsnart
- 47
- 5