我有一个设置为十进制的数据库字段,而在我的 Go 项目中,我在选择可以使用哪种数据类型时遇到问题。 每次我向代码发送创建请求时,都会收到“无法将‘小数’编码到#golang datatype#
”这是我的数据库架构
CREATE TABLE transaction(
organization_id timeuuid,
month text,
employee_id timeuuid,
id timeuuid,
employee_name text,
amount decimal,
deductions int,
date_approved date,
PRIMARY KEY ((organization_id, month), id)
)
我的 golang 模型看起来像这样
type WageGarnishment struct {
ID gocql.UUID `json:"id"`
organizationID gocql.UUID `json:"organization_id"`
Month string `json:"month"`
Amount Float64 `json:"amount"`
Deductions uint `json:"deductions"`
EffectiveDate time.Time `json:"effective_date"`
DateApproved time.Time `json:"date_approved"`
EmployeeSummary
}
无论我的金额字段中的数据类型如何,我都会收到此错误:
Error #01: can not marshal float64 into decimal
感谢您提前提供帮助
最佳答案
如果你查看documentation for Gocql package ,然后您将看到 decimal
映射到 Go 的 infDec
数据类型(请参阅 its doc ),因此您需要使用它而不是 Float64
.
关于database - 有没有办法在Golang中实现cassandra "decimal"数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71125652/