3 ms·
Is Go's character type signed? If so, it's possible to make p negative, in which case this wouldn't apply: if p >= len(em) { panic(fmt.Spr
by comex 15y ago
Is Go's character type signed? If so, it's possible to make p negative, in which case this wouldn't apply:
if p >= len(em) {
panic(fmt.Sprintf("element index out of range %s: "+
"%d >= %d", t, p, len(em)))
}
although Go would still do its own runtime check for the dereference.
- BarkMore 15y agoThe variable "p" is a rune. Although the rune type can have negative values, p will only have values >= 0 in this code.
- comex 15y agoHow so? If the character is any value less than '0' (say, '!'), p will be set to a negative value.
- BarkMore 15y ago'0' has the decimal value 48, not zero. '!' has the decimal value 33. Here's a demo program that you can run from your browser: http://play.golang.org/p/qAKG9j5E4V http://play.golang.org/p/qAKG9j5E4V All characters have values greater than or equal to zero.
- Dylan16807 15y agocomex said less than '0'. 33 minus 48 is negative.