3 ms·
In Go, you can name your variables in Chinese† if you want. In Go, strings are immutable UTF-8 byte arrays,†† and the language provides facilities for iteratin
by cynwoody 13y ago
In Go, you can name your variables in Chinese† if you want.
In Go, strings are immutable UTF-8 byte arrays,†† and the language provides facilities for iterating over them either byte by byte or rune by rune (a rune is an int32, wide enough to hold any unicode character).
†http://play.golang.org/p/skyEYhxWg3 http://play.golang.org/p/skyEYhxWg3
††http://blog.golang.org/strings http://blog.golang.org/strings
- dbaupp 13y agoDoes Go provide functions for iterating grapheme by grapheme?
- MBCook 13y agoThat's the purpose of the Rune type, I believe. When you iterate over text, it gives you one character at a time, not a fixed number of bytes.
- pjscott 13y agoThat's iteration by code point, not by grapheme. For example, the grapheme 'ä' may be represented as two code points: an 'a' and a combining diaeresis. http://en.wikipedia.org/wiki/Code_point http://en.wikipedia.org/wiki/Code_point http://en.wikipedia.org/wiki/Grapheme http://en.wikipedia.org/wiki/Grapheme
- cynwoody 13y agoThat's being worked on†. It hasn't made it into the standard package library as far as I know. E.g., there are two ways to write Cañyon City. You can write the ñ as U+00F1 or as an ascii lower-case n followed by a combining tilde (U+0303). The first case results in a single rune, and the second in two runes. Example††. You need additional logic in order to normalize to a canonical representation and realize that the two strings are actually the same. Also, if you are displaying the string, you need to account for the fact that, although the two strings have different byte and rune lengths, they take up exactly the same number of pixels on your display medium. †http://blog.golang.org/normalization http://blog.golang.org/normalization ††http://play.golang.org/p/XJPydELZ6s http://play.golang.org/p/XJPydELZ6s
- coldtea 13y ago>E.g., there are two ways to write Cañyon City. You can write the ñ as U+00F1 or as an ascii lower-case n followed by a combining tilde (U+0303). The first case results in a single rune, and the second in two runes. Example††. You need additional logic in order to normalize to a canonical representation and realize that the two strings are actually the same. Who thought that having two ways to go about this was a good idea in the first place?
- dsymonds 13y agoA Go string is an immutable opaque sequence of bytes. It is conventionally UTF-8, but it does not have to be. The only place in the language that cares is if you range over a string, but even there non-UTF-8 strings are supported.
- Grue3 13y agoCan you have spaces or other reserved characters in variable names? Because in Lisp you can. In addition to being able to use characters from any encoding of course.