3 ms·
Golang Quirk: Number-strings starting with “0” are Octals
- zcdopizocioics 8y agoThe strconv function simply acts in the same manner as the language does, see https://play.golang.org/p/rg0VwhCDVB_C https://play.golang.org/p/rg0VwhCDVB_C The idea for strconv originates in c (see atoi etc), and c also has 0-octal notation. Go is simply mimicing c in this regard. > What was the Golang team thinking?! They were helping you not misuse the function, as you are doing in your example (you are trying to parse strings containing invalid number representations as integers).
- kaushalmodi 8y agoThanks for your comment. It kind of makes a little sense now. I just wished the octals needed a stricter prefix like "0o" as they are not very common compared to decimal and hex (in my experience at least). The way Python casts strings with leading zeros makes more sense to me. Later I'll update the post with your feedback. Thanks.
- karmakaze 8y ago> If base == 0, the base is implied by the string’s prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise. For bases 1, below 0 or above 36 an error is returned. A better solution is to always provide the base (e.g. 10) rather than using 0 if you don't want special interpretation of '0x' and '0' prefixes. That should be in Hugo. The problem is not with the golang library which was well documented and always worked as such. By placing any blame on the golang library, there's less incentive to fix Hugo.
- kaushalmodi 8y agoThat's a valid point. I have made that suggestion to the Hugo devs[1]. Someone on Reddit also suggested using strconv.Atoi() instead of ParseInt(). I have added to the suggestion too. [1]: https://github.com/gohugoio/hugo/issues/4628#issuecomment-382595019 https://github.com/gohugoio/hugo/issues/4628#issuecomment-38...
- kaushalmodi 8y agoFollow-up post: https://scripter.co/follow-up-golang-quirk-number-strings-starting-with-0-are-octals/ https://scripter.co/follow-up-golang-quirk-number-strings-st...