Why does Go treat a Postgresql numeric & decimal columns as []uint8?
Because there is no better solution. (At least wasn't until Go 1.5's big.Float
). What other alternatives are there?
Turn it into an integer. Obviously a bad solution since numerics can have a fractional part.
Turn it into a
float64
. This is evil. Especially if you're working with money (where types likenumeric
anddecimal
have the most usage).
This particular database driver chooses instead to return a string containing the number - to let you decide, whether to lose some precision (by converting it into a float64
with strconv
) or use a decimal/precise number library (like gmp
or math/big
).
Here is an issue with the answer to that questions from the developers: https://github.com/lib/pq/issues/648
It's not safe to use a float64 type for a decimal because floats can't represent all decimals. For example, decimals support exponents much larger than floats, and the coefficients of floats, when converted to their base-2 representation, will change. It is trivial in go to convert a string to a float, so we are going to keep this behavior.