Tahseen
Tahseen

Reputation: 1218

Golang type for Cassandra Decimal

Sorry for asking this question but after being tired am writing it here.

I am not able to map any variable type of Golang to Cassandra's Decimal type, which is multi precision

I used interface {}, which works so long as I don't place value in the variable. The moment I do, it has issue

Upvotes: 2

Views: 955

Answers (2)

Karl Anthony Baluyot
Karl Anthony Baluyot

Reputation: 176

You may use the infinite-precision decimal package and import this gopkg directly,

import "gopkg.in/inf.v0"

Let's say you have a particular model that reflects or binds to your Scylla/Cassandra table with a decimal data type,

type TradeModel struct {
    Balance *inf.Dec
}

To cast or convert your string to this decimal data type, you may use this function:

func ToDecimal(s string) *inf.Dec {
    d := new(inf.Dec)

    res, _ := d.SetString(s)
    return res
}

Take a look with these examples: https://github.com/go-inf/inf/blob/master/dec.go

NOTE: I did not explicitly write the second result from the return value of SetString for simplicity. That argument will return a boolean whether the conversion is success or not which is important (in my case this is already in direct helpers).

Upvotes: 5

Tahseen
Tahseen

Reputation: 1218

https://github.com/gocql/gocql/blob/master/helpers.go

As per this link I found that inf.Dec type of Golang maps to Cassandra's Decimal

var decimal inf.Dec

or

decimal := inf.NewDec(1, 0)

Upvotes: 0

Related Questions