/dosh

🚧 A Go library for representing monetary values in-process and on-the-wire.

Primary LanguageGoMIT LicenseMIT

Dosh

A Go module for representing, manipulating and serializing monetary values.

Documentation Latest Version Build Status Code Coverage

The API is centered around the Amount type, which is an immutable monetary value in a specific currency. It is based on Shopspring's arbitrary-precision Decimal type, making it suitable for mathematical operations.

Serialization

Amount provides serialization logic for text, binary, JSON and protocol buffers formats.

Both the text and binary formats are "lossless" insofar as they can encode any amount that can be represented by the internal arbitrary precision decimal.

The JSON and protocol buffers formats are based on the google.type.Money "well-known" protocol buffers type, which has a fixed precision of 9 decimal places.

The JSON representation of an amount is obtained by applying Protocol Buffers' canonical JSON mapping rules to the Money type. This encoding is widely used throughout Google's APIs, an example of which can be seen here.

Dosh also provides the protomoney package, which can be used to perform comparisons and basic mathematical operations on google.type.Money values directly, without unmarshaling them to an Amount.

Caveats

Google's money package does not include the source .proto file used to generate the money.Money type. This makes it difficult to use the Money type in user-defined protocol buffers messages. For this reason, the original .proto file is included in Dosh's protomoney package.