Is there a way to implement cassandra "decimal" Datatype in Golang

Issue

i have a database field that is set to decimal, while in my Go project i am having problem choosing which datatype can be use.
each time i send a create reuquest to my code, i get a "cannot marshal ‘decimal’ into #golang datatype#

this my database schema

CREATE TABLE wage_garnishment(
organization_id timeuuid,
yyyy text,
employee_id timeuuid,
id timeuuid,
employee_name text,
amount decimal,
deductions int,
date_created date,
date_modified date,
date_approved date,
PRIMARY KEY ((organization_id, yyyy), id)

)

my golang models looks like this

type WageGarnishment struct {
ID            gocql.UUID `json:"id"`
organizationID            gocql.UUID `json:"organization_id"`
Yyyy          string     `json:"yyyy"`
Amount        Float64    `json:"amount"`
Deductions    uint       `json:"deductions"`
EffectiveDate time.Time  `json:"effective_date"`
DateCreated   time.Time  `json:"date_created"`
DateApproved  time.Time  `json:"date_approved"`
DateModified  time.Time  `json:"date_modified"`
EmployeeSummary

}

no matter the datatype in my Amount Field I keep getting this Error:

Error #01: can not marshal float64 into decimal

Thansks for your Help in Advance

Solution

If you look into documentation for Gocql package, then you will see that the decimal is mapped to the Go’s infDec data type (see its doc) so you need to use it instead of Float64.

Answered By – Alex Ott

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.