Go-units setup question

I’m using Go-units and I can see that there’s the ability to add in new units with new conversion ratios, but I don’t know how to take advantage of this in my code.

I’m presuming that I put a file somewhere which contains the new types and conversion ratios, but I’m lost as to where and how to stitch it into my running code.

On their Github page they have a section for custom units:

// register custom unit names
Ding := u.NewUnit("ding", "di")
Dong := u.NewUnit("dong", "do")

// there are 100 dongs in a ding
u.NewRatioConversion(Ding, Dong, 100.0)

val := u.NewValue(25.0, Ding)

fmt.Printf("%s = %s\n", val, val.MustConvert(Dong)) // "25 dings = 2500 dongs"

// conversions are automatically registered when using magnitude prefix helper methods
KiloDong := u.Kilo(Dong)
fmt.Println(u.MustConvertFloat(1000.0, Dong, KiloDong)) // "1 kilodong"

Did you try something like that?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.