Answer the question
In order to leave comments, you need to log in
Why does golang only encode a string character in byte(uint8) and rune(int32)?
Good afternoon.
Question with an example, we have:
charLine := "слово"
fmt.Println(charLine) // кодирует кириллицу в по 2 байта на каждый символ. По дефолту тип элементов(символов) byte т.е. uint8
sliceLine := []byte(charLine)
fmt.Println(sliceLine) // byte это алиас на тип uint8, т.е. сейчас видим бинарное представление данных, как сделано для типа string с приведением к строке. Делая срез мы можем указать какой тип использовать: uint8 или int32 (rune)
Answer the question
In order to leave comments, you need to log in
What are you actually trying to achieve with these actions?
When it comes to memory, Go's internal representation of strings is UTF-8. When you write charLine := "слово"
, you are specifying a UTF-8 string.
In Go, a string can be converted (back and forth) to only two types - to []byte and to []rune. []byte is a mutable copy of the bytes of the immutable string, []rune is the result of parsing the UTF-8 bytes of the string into 4-byte unicode codes.
The question why only these two, and not, say, []float, is meaningless. Because that's how language is made. If you need []uint16 - well, write your own converter, it's not difficult.
It's all described in the documentation here: golang.org/ref/spec#Conversions_to_and_from_a_stri...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question