Documentation
¶
Overview ¶
Package money provides a simple and efficient way to handle monetary values in Go.
Index ¶
- Variables
- type Currencies
- type Currency
- type Formatter
- type Money
- func (m Money) Absolute() Money
- func (m Money) Add(om Money) (Money, error)
- func (m Money) Allocate(ratios ...int) ([]Money, error)
- func (m Money) AsMajorUnits() float64
- func (m Money) Display() string
- func (m Money) Equals(om Money) (bool, error)
- func (m Money) GreaterThan(om Money) (bool, error)
- func (m Money) LessThan(om Money) (bool, error)
- func (m Money) MarshalJSON() ([]byte, error)
- func (m Money) Multiply(mul int) Money
- func (m Money) SameCurrency(other Money) bool
- func (m *Money) Scan(src any) error
- func (m Money) Split(n int) ([]Money, error)
- func (m Money) Subtract(om Money) (Money, error)
- func (m *Money) UnmarshalJSON(b []byte) error
- func (m Money) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
var ( AED = currencyCode{"AED"} AFN = currencyCode{"AFN"} ALL = currencyCode{"ALL"} AMD = currencyCode{"AMD"} ANG = currencyCode{"ANG"} AOA = currencyCode{"AOA"} ARS = currencyCode{"ARS"} AUD = currencyCode{"AUD"} AWG = currencyCode{"AWG"} AZN = currencyCode{"AZN"} BAM = currencyCode{"BAM"} BBD = currencyCode{"BBD"} BDT = currencyCode{"BDT"} BGN = currencyCode{"BGN"} BHD = currencyCode{"BHD"} BIF = currencyCode{"BIF"} BMD = currencyCode{"BMD"} BND = currencyCode{"BND"} BOB = currencyCode{"BOB"} BRL = currencyCode{"BRL"} BSD = currencyCode{"BSD"} BTN = currencyCode{"BTN"} BWP = currencyCode{"BWP"} BYN = currencyCode{"BYN"} BYR = currencyCode{"BYR"} BZD = currencyCode{"BZD"} CAD = currencyCode{"CAD"} CDF = currencyCode{"CDF"} CHF = currencyCode{"CHF"} CLF = currencyCode{"CLF"} CLP = currencyCode{"CLP"} CNY = currencyCode{"CNY"} COP = currencyCode{"COP"} CRC = currencyCode{"CRC"} CUC = currencyCode{"CUC"} CUP = currencyCode{"CUP"} CVE = currencyCode{"CVE"} CZK = currencyCode{"CZK"} DJF = currencyCode{"DJF"} DKK = currencyCode{"DKK"} DOP = currencyCode{"DOP"} DZD = currencyCode{"DZD"} EEK = currencyCode{"EEK"} EGP = currencyCode{"EGP"} ERN = currencyCode{"ERN"} ETB = currencyCode{"ETB"} EUR = currencyCode{"EUR"} FJD = currencyCode{"FJD"} FKP = currencyCode{"FKP"} GBP = currencyCode{"GBP"} GEL = currencyCode{"GEL"} GGP = currencyCode{"GGP"} GHC = currencyCode{"GHC"} GHS = currencyCode{"GHS"} GIP = currencyCode{"GIP"} GMD = currencyCode{"GMD"} GNF = currencyCode{"GNF"} GTQ = currencyCode{"GTQ"} GYD = currencyCode{"GYD"} HKD = currencyCode{"HKD"} HNL = currencyCode{"HNL"} HRK = currencyCode{"HRK"} HTG = currencyCode{"HTG"} HUF = currencyCode{"HUF"} IDR = currencyCode{"IDR"} ILS = currencyCode{"ILS"} IMP = currencyCode{"IMP"} INR = currencyCode{"INR"} IQD = currencyCode{"IQD"} IRR = currencyCode{"IRR"} ISK = currencyCode{"ISK"} JEP = currencyCode{"JEP"} JMD = currencyCode{"JMD"} JOD = currencyCode{"JOD"} JPY = currencyCode{"JPY"} KES = currencyCode{"KES"} KGS = currencyCode{"KGS"} KHR = currencyCode{"KHR"} KMF = currencyCode{"KMF"} KPW = currencyCode{"KPW"} KRW = currencyCode{"KRW"} KWD = currencyCode{"KWD"} KYD = currencyCode{"KYD"} KZT = currencyCode{"KZT"} LAK = currencyCode{"LAK"} LBP = currencyCode{"LBP"} LKR = currencyCode{"LKR"} LRD = currencyCode{"LRD"} LSL = currencyCode{"LSL"} LTL = currencyCode{"LTL"} LVL = currencyCode{"LVL"} LYD = currencyCode{"LYD"} MAD = currencyCode{"MAD"} MDL = currencyCode{"MDL"} MGA = currencyCode{"MGA"} MKD = currencyCode{"MKD"} MMK = currencyCode{"MMK"} MNT = currencyCode{"MNT"} MOP = currencyCode{"MOP"} MUR = currencyCode{"MUR"} MRU = currencyCode{"MRU"} MVR = currencyCode{"MVR"} MWK = currencyCode{"MWK"} MXN = currencyCode{"MXN"} MYR = currencyCode{"MYR"} MZN = currencyCode{"MZN"} NAD = currencyCode{"NAD"} NGN = currencyCode{"NGN"} NIO = currencyCode{"NIO"} NOK = currencyCode{"NOK"} NPR = currencyCode{"NPR"} NZD = currencyCode{"NZD"} OMR = currencyCode{"OMR"} PAB = currencyCode{"PAB"} PEN = currencyCode{"PEN"} PGK = currencyCode{"PGK"} PHP = currencyCode{"PHP"} PKR = currencyCode{"PKR"} PLN = currencyCode{"PLN"} PYG = currencyCode{"PYG"} QAR = currencyCode{"QAR"} RON = currencyCode{"RON"} RSD = currencyCode{"RSD"} RUB = currencyCode{"RUB"} RUR = currencyCode{"RUR"} RWF = currencyCode{"RWF"} SAR = currencyCode{"SAR"} SBD = currencyCode{"SBD"} SCR = currencyCode{"SCR"} SDG = currencyCode{"SDG"} SEK = currencyCode{"SEK"} SGD = currencyCode{"SGD"} SHP = currencyCode{"SHP"} SKK = currencyCode{"SKK"} SLE = currencyCode{"SLE"} SLL = currencyCode{"SLL"} SOS = currencyCode{"SOS"} SRD = currencyCode{"SRD"} SSP = currencyCode{"SSP"} STD = currencyCode{"STD"} STN = currencyCode{"STN"} SVC = currencyCode{"SVC"} SYP = currencyCode{"SYP"} SZL = currencyCode{"SZL"} THB = currencyCode{"THB"} TJS = currencyCode{"TJS"} TMT = currencyCode{"TMT"} TND = currencyCode{"TND"} TOP = currencyCode{"TOP"} TRL = currencyCode{"TRL"} TRY = currencyCode{"TRY"} TTD = currencyCode{"TTD"} TWD = currencyCode{"TWD"} TZS = currencyCode{"TZS"} UAH = currencyCode{"UAH"} UGX = currencyCode{"UGX"} USD = currencyCode{"USD"} UYU = currencyCode{"UYU"} UZS = currencyCode{"UZS"} VEF = currencyCode{"VEF"} VES = currencyCode{"VES"} VND = currencyCode{"VND"} VUV = currencyCode{"VUV"} WST = currencyCode{"WST"} XAF = currencyCode{"XAF"} XAG = currencyCode{"XAG"} XAU = currencyCode{"XAU"} XCD = currencyCode{"XCD"} XCG = currencyCode{"XCG"} XDR = currencyCode{"XDR"} XOF = currencyCode{"XOF"} XPF = currencyCode{"XPF"} YER = currencyCode{"YER"} ZAR = currencyCode{"ZAR"} ZMW = currencyCode{"ZMW"} ZWD = currencyCode{"ZWD"} ZWL = currencyCode{"ZWL"} )
Functions ¶
This section is empty.
Types ¶
type Currencies ¶
type Currencies map[currencyCode]Currency
Currencies is a map of currency codes to their corresponding currency details.
type Currency ¶
type Currency struct {
Code currencyCode
NumericCode string
Fraction int
Grapheme string
Template string
Decimal string
Thousand string
}
Currency represents the details of a Currency.
func (Currency) Formatter ¶
Formatter returns a Formatter struct based on the currency's formatting details.
type Formatter ¶
type Formatter struct {
Fraction int
Decimal string
Thousand string
Grapheme string
Template string
}
Formatter stores Money formatting information.
func (Formatter) Format ¶
Format returns a string representation of the amount using the currency formatting rules.
func (Formatter) ToMajorUnits ¶
ToMajorUnits returns the amount as a float64 in major units (e.g. cents -> dollars). Use for display purposes only, never for arithmetic.
type Money ¶
Money represents a monetary value. Fields are exported for convenience. Prefer using the provided methods over direct field mutation to ensure correct behavior.
func (Money) Absolute ¶
Absolute returns a new Money instance with the absolute value of the amount.
func (Money) Add ¶
Add adds another Money instance to this Money instance and returns the result. It returns an error if the currencies of the two Money instances are different.
func (Money) Allocate ¶
Allocate divides the amount of this Money instance according to the given ratios and returns a slice of Money instances. It returns an error if no ratios are specified or if any ratio is negative.
func (Money) AsMajorUnits ¶
AsMajorUnits returns the amount as a float64 in major units (e.g. cents -> dollars). Use for display purposes only, never for arithmetic.
func (Money) Display ¶
Display returns a string representation of the amount using the currency formatting rules.
func (Money) GreaterThan ¶
GreaterThan checks if this Money instance is greater than another Money instance.
func (Money) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to allow Money instances to be serialized to JSON.
func (Money) Multiply ¶
Multiply multiplies the amount of this Money instance by a given multiplier and returns the result.
func (Money) SameCurrency ¶
SameCurrency checks if the currency of this Money instance is the same as another Money instance.
func (*Money) Scan ¶
Scan implements sql.Scanner for database deserialization. Expects the amount as int64 — store currency code separately.
func (Money) Split ¶
Split divides the amount of this Money instance into n equal parts and returns a slice of Money instances. It returns an error if n is less than or equal to zero.
func (Money) Subtract ¶
Subtract subtracts another Money instance from this Money instance and returns the result. It returns an error if the currencies of the two Money instances are different.
func (*Money) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to allow Money instances to be deserialized from JSON.
