repositorypackage
0.0.0-20211104122029-2119dfde5e13
Repository: https://github.com/amupxm/go-money.git
Documentation: pkg.go.dev
# Packages
No description provided by the author
# README
Go Money
Golang pure money calculation library provides safety and ease of use.
Usage
go-money can parse money string to money type but you can use numbers as input too.
moneyOne, _ := money.ParseCAD("-$100.90")
moneyTwo, _ := money.ParseCAD(".90$")
result := moneyOne.Sub(*moneyTwo)
fmt.Printf("money is : %v\n", result) // will prints money is : CAD$-101.80
You can pass all valid money string like $CAD-9.0 , CAD$79 , 23.65 , -0.9 , .09 , 78.98CAD , 98.07$ and ...
It also provides inner umarshaler,unmarshaler,Scanner and valuer which make it super easy to use
unmarshal
type jsonType struct {
User string `json:"user"`
Money money.CAD `json:"money"`
}
var someStruct jsonType
someJsonString := `{"user":"amupxm","money":"-78.23"}`
json.Unmarshal([]byte(someJsonString), &someStruct)
fmt.Printf("Unmarshaled money as cents : %v\n", someStruct.Money.AsCent()) // Unmarshaled money as cents : -7823
marshal
data := struct {
Type string `json:"type"`
Money money.CAD `json:"money"`
}{
Type: "Offer",
Money: *money.NewMoney(99, 99),
}
b, _ := json.Marshal(data)
fmt.Printf("Marshaled money is : %v\n", string(b)) // Marshaled money is : {"type":"Offer","money":"CAD$99.99"}
And you can scan,value to sql/database without any troubles =)
var theMoney money.CAD
data := struct {
UserName string `json:"user_name"`
Money money.CAD `json:"money"`
}{
UserName: "amupxm",
Money: *money.NewMoney(99, 99),
}
err := pg.db.QueryRow(`INSERT INTO transactions (username, money)
VALUES ($1, $2) RETURNING money`, user.UserName,user.money).Scan(&theMoney)
fmt.Println("Money is %v\n" ,theMoney ) // Money is : CAD$99.99
Money type in compatible with postgres monet type.
Give star if interested :star2: