Re:Strings, bytes, runes and characters in Go
by twotwotwo on Wednesday October 23, 2013 @10:12PM
(#6603020)
Just for fun, here's Go handling a char outside the BMP (š, U+1F603):
Re:Strings, bytes, runes and characters in Go
by samatman on Thursday October 24, 2013 @12:02AM
(#6603344)
https://github.com/mnemnion/emojure/
You can even export them without Capital letters ;-)
Re:Strings, bytes, runes and characters in Go
by twotwotwo on Thursday October 24, 2013 @01:20AM
(#6603527)
I did run into at least one eminently reasonable use of how Go source is defined to be in UTF-8. Comments in the crypto libs just use math symbols where they're handy, like this in crypto/rsa[1]:
// Check that de ā” 1 mod p-1, for each prime.
// This implies that e is coprime to each p-1 as e has a multiplicative
// inverse. Therefore e is coprime to lcm(p-1,q-1,r-1,...) =
// exponent(ā¤/nā¤). It also implies that a^de ā” a mod p as a^(p-1) ā” 1
// mod p. Thus a^de ā” a mod n for all a coprime to n, as required.
Sadly, the spec requires identifiers to be just Unicode letters and digits, so we will never experience the power and glory of emoji function names in Go.
That's a hard problem, and avoiding it in every situation would require scanning the strings for surrogates beforehand, when you might never need to know that information. Go makes it explicit that knowing the exact character position and string length in characters comes at a cost.
There's a good discussion of this on Tim Bray's blog: http://www.tbray.org/ongoing/When/200x/2003/04/26/UTF