A string
in go is a collection of immutable byte
s. A byte
is an alias for uint8
. A rune
is an alias for int32
that is used to store characters.
Why do rune
s use int32
s, instead of uin32
s? There is no such thing known as a negative character.
string
s use byte
s, in which each byte
is enough to store ascii characters, but not unicode characters. How ever, go can store unicode characters in strings, but indexing a character it loses it's data. You can't convert a float64
to an int
implicitly in go, since it might lose that, but this conversion of indexing a string
, containing a unicode character, does not raise any errors and just loses its data. How can I index a rune
out of a string
, instead of a byte
?
Consider the following program and its output.
package main
import (
"fmt"
)
func main() {
x := "ඞ"
y := x[0]
z := 'ඞ'
fmt.Printf("%s vs %c vs %c\n", x, y, z)
}
ඞ vs à vs ඞ
What I feel like a string
does for storing unicode characters is combining bytes, since it's possible to index 1 out of x
as well.