gop icon indicating copy to clipboard operation
gop copied to clipboard

More information about the `string` type in documentation

Open telemachus opened this issue 2 years ago • 0 comments

Proposal

Please consider adding more information about strings in the documentation.

The current example uses "Bob" as a string, and this simplifies things since it is all ASCII.

However, consider the following:

name := "Hello, 世界"
println name.len      // 13 (This will likely surprise many new users, especially non-programmers.)
println name[0]       // 72 (This will likely surprise many new users, especially non-programmers.)
println name[7]       // 228 (This will likely confuse a lot of people, including programmers.)
println name[7:13]    // 世界 (13 will confuse many.)
println name[7:10]    // 世 (10 will confuse many.)
println name[7:9]     // � (Many users will try something like this and be confused by the result.)

It would be great if the documentation said more about the complex interplay between strings, runes, and Unicode.

Background

I was reading the documentation, and I was confused. The strings tutorial has the same problem because it also uses only ASCII characters.

In particular, I was initially confused by this:

println name[0]   // 66

I realized quickly that this was a decimal representation of an ASCII character, but I wondered two things. First, why does a single string index return a numeric representation of a character rather than the character itself? (Compare, e.g., Python: a = 'Bob'; print(a[0]) # 'B'.) Second, what happens to that numeric representation if the string is outside the ASCII range?

This all makes reasonable sense if you come from Go and are familiar with strings, runes, and Unicode there, but for newcomers it will likely be confusing.

Workarounds

I'm not aware of any workaround right now.

telemachus avatar Jun 11 '22 16:06 telemachus