Go: proposal: Go 2: introduce a money package

Created on 7 Dec 2018  ·  4Comments  ·  Source: golang/go

Introduce a money package in Go2

Based on Martin Fowler:
A large proportion of the computers in this world manipulate money, so it's always puzzled me that money isn't actually a first class data type in any mainstream programming language. The lack of a type causes problems, the most obvious surrounding currencies. If all your calculations are done in a single currency, this isn't a huge problem, but once you involve multiple currencies you want to avoid adding your dollars to your yen without taking the currency differences into account. The more subtle problem is with rounding. Monetary calculations are often rounded to the smallest currency unit. When you do this it's easy to lose pennies (or your local equivalent) because of rounding errors.

The good thing about object-oriented programming is that you can fix these problems by creating a Money class that handles them. Of course, it's still surprising that none of the mainstream base class libraries actually do this.

https://martinfowler.com/eaaCatalog/money.html
https://martinfowler.com/bliki/CurrencyAsValue.html

FrozenDueToAge Go2 Proposal

Most helpful comment

Might be useful to look at the golang.org/x/text/currency package.

This proposal seems to lack an actual proposal. What API should the package provide?

All 4 comments

How to design the money package?

I assume this proposal can only solve the money problem.
If this proposal need change the golang syntax, than it may make more problems than it solves. (like everyone use the golang need to learn this syntax to understand other's code)
If this proposal do not need change the golang syntax, then it will be better to implement outside GOROOT.

Might be useful to look at the golang.org/x/text/currency package.

This proposal seems to lack an actual proposal. What API should the package provide?

I think you are talking less about currency and more about decimals. there are some existing issues such support.

If you have decimal needs right now, you may want to look into https://godoc.org/github.com/cockroachdb/apd .
Also related #19787 and #12127.

Thanks, but in order to move forward we need a specific proposal with an API. At the moment it seems to me that this can be done entirely in a library, and that library doesn't need to be in the standard library. Closing for now but please feel free to comment.

Was this page helpful?
0 / 5 - 0 ratings