jackc / pgtype

MIT License
308 stars 110 forks source link

Assign Numeric to *big.Rat #143

Closed olomix closed 2 years ago

olomix commented 2 years ago

When I need to represent Numeric types in Go, I use big.Rat often. Added to Numeric.AssignTo ability to assign to big.Rat variables. I don't see corner cases when it should not work. Personally I use wrapper functions that do it internally for a long time. So I hope it would be helpful.

Example

var r = new(big.Rat)
var n pgtype.Numeric
err = n.AssignTo(r)
jackc commented 2 years ago

What is the domain where it makes more sense to use rationals instead of a decimal type? (not specifically against supporting *big.Rat, just never needed it myself and it hasn't come up before)

olomix commented 2 years ago

Go does not have decimal type in standard library. And *big.Rat is a decent replacement when you need exact precision.

If you use external dependency for decimals (like shopspring/decimal), then you have support in one database driver (jackc/pgtype), but have no this support in another (ClickHouse/clickhouse-go).

Also *big.Rat is more of less guaranteed to be supported "forever" in future versions of Go.

That's why I prefer to go with *big.Rat.