rune

What is a rune?

旧巷老猫 提交于 2019-11-29 18:53:56
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)

Access random rune element of string without using for … range

落花浮王杯 提交于 2019-11-29 16:46:25
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 string . One way is: func RuneElement(str string, idx int) rune { var ret rune for i, c := range str { if i == idx { return c } } return ret // out of range -> proper handling is needed } What if I want to call such a function a lot of times? I guess what I am looking for is like an operator/function like str[i] (which returns a byte ) that return the

Does accessing elements of string as byte perform conversion?

删除回忆录丶 提交于 2019-11-29 16:19:32
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, but I am not sure. If so, then, which one of the following methods are better performance-wise? Is one preferred over another (in terms of best practice, for example)? str := "large text" for i := range str { // use str[i] } or str := "large text" str2 := []byte(str) for _, s := range str2 { // use s } Which one of the following methods are better

Access random rune element of string without using for … range

余生长醉 提交于 2019-11-28 11:04:10
问题 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 string . One way is: func RuneElement(str string, idx int) rune { var ret rune for i, c := range str { if i == idx { return c } } return ret // out of range -> proper handling is needed } What if I want to call such a function a lot of times? I guess

Does accessing elements of string as byte perform conversion?

风流意气都作罢 提交于 2019-11-28 09:48:21
问题 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, but I am not sure. If so, then, which one of the following methods are better performance-wise? Is one preferred over another (in terms of best practice, for example)? str := "large text" for i := range str { // use str[i] } or str := "large text" str2