When your string contains Unicode characters dealing with it as individual bytes is difficult. When using runes with range - each index is a “Unicode character” (which may be multiple bytes) - which is easy to use.
See go.dev/blog/strings > On Nov 15, 2021, at 12:00 PM, Kamil Ziemian <kziemian...@gmail.com> wrote: > > Hello, > > I read quite a few blog posts, articles, listen to nice number to talks about > strings, runes and encoding in Go. I now reading Go Language Spec and I just > stuck in the section about runes. I mean, it isn't hard as itself, but it > raises to much questions to me. I decided that I need to learn more about > Unicode and UTF-8, so from today I'm reading Unicode Technical Site (?), > currently the Glossary (https://www.unicode.org/glossary/). But I can't > understand one thing: when in practice you should use runes? > > 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. 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. 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? In > Go stdlib there is a RuneReader interface (?) so this reason must exists, but > I just can't find anything. Maybe it have something to do with sending > information using internet? I don't know, this is totally outside my humble > knowledge. > > You can say, that since I don't see a reason to use runes, I probably > shouldn't care about it. This is a valid point, but I want to know Go > reasonable well and constantly find code with runes which reason of existence > I don't understand (e.g. functions in stdlib that operates on runes) is quite > demoralising to me. > > Best > Kamil > -- > 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/f3dad0e1-cd25-4e33-a7f2-34e0118bf68an%40googlegroups.com. -- 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/649D31F8-C9DB-4C03-A306-D18FF2C8E954%40ix.netcom.com.