On Mon, Nov 15, 2021 at 7:00 PM Kamil Ziemian <kziemian...@gmail.com> wrote:
> ... when in practice you should use runes? For example the API of the unicode package uses runes extensively: https://pkg.go.dev/unicode. > My understanding at this moment is like that. Unicode assign every symbol a > number (at this moment I disregard normalization and any other more advance > stuff), rune is alias for int32 that stores integer representation of this > number. Unicode text is a sequence of codepoints. Some represent [visible] glyphs, some do not. Codepoint is just a numeric code, like 32 is the numeric code of the ASCII space symbol. (Unicode is a superset of ASCII.) > UTF-8 is variable size encoding using one or more bytes to encode symbol and > shouldn't and DOESN'T represent integer value of symbols Unicode number. UTF-8 encded Unicode text represents those codepoint sequences most of the time in smaller space, losslessly. UTF-8 encoded codepoint still represents the Unicode codepoint ie. it is a numeric value. But semantically, not directly in the bit pattern of the byte sequence. > Virtues of UTF-8 are clear as how it allows to save a space is clear to me, > but I can't find a reason why I should transform my text to runes? No need to search for a reason. It'll run into you sooner or later while you keep writing non-trivial, Unicode aware code ;-) But if I would have to come with an example: Consider a text editor that reads a file, splits it in lines, which are UTF-8 strings and keeps them in this encoded form for space efficiency. While editing a line, it is however probably better to convert the current/edited line into a rune slice (lineRunes := []rune(line[cursorY])) when the cursor enters the line and work with that form until the cursor moves to a different line at which moment the []rune will be converted back to a string (line[cursorY] = string(lineRunes)). -j -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA40n-VrZmr_G0qhd6M%2B_cy6wg5_PLpNaiNhtgp%3DSCjDGRH9tA%40mail.gmail.com.