gocastingpanicstrconv

Why I do get Go error "panic: strconv: illegal AppendInt/FormatInt base"


I can't figure out, why I do get this error message during run time. It triggers even with the most simple one-liner: strconv.FormatUint(uint64(123), 64)

Have I understood something very wrong here? The code compiles just fine.

EDIT: Found a solution (as later pointed out by Tim Cooper). I think the examples in the documentation are confusing:

s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)

When you only use base 64 and 16 in the examples, it is quite easy to make the wrong conclusion. However, now that I know what was wrong, the error message makes much more sense.


Solution

  • Base 64 is not supported, as specified in the documentation:

    func FormatUint(i uint64, base int) string
    

    FormatUint returns the string representation of i in the given base, for 2 <= base <= 36.