Does GORM have a decimal datatype to store money values (-> Decimal(8,2)
)?
I could not find it on https://github.com/jinzhu/gorm#define-models-structs
If you're using AutoMigrate, you can give GORM SQL instructions (in your struct model) on how to build the table. Try something like the following:
type Product struct {
Id int
ProductName string `sql:"type:varchar(250);"`
Amount float32 `sql:"type:decimal(10,2);"`
}
I know this is a little old but I had this issue and it's very hard to find the answer. if you're using Gorm with liquibase use BigDecimal for any floating point numbers.
Michael's answer works. But If you want to use decimal type with golang, you can use shopspring/decimal like this:
type TableName struct {
Amount decimal.Decimal `json:"amount" sql:"type:decimal(20,8);"`
}