diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2022-05-15 16:51:41 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2022-05-15 16:51:41 -0230 |
| commit | 585fbf852c1e76470df42ebe99ede62440ce19d9 (patch) | |
| tree | 4bb5406eb0e1d1a978e21de1e6c4291c22510d39 /volume | |
| parent | 98a5ded8daccbb576b2928359112c6454fd0c5b3 (diff) | |
| download | volute-585fbf852c1e76470df42ebe99ede62440ce19d9.zip | |
simplify unit conversion
Diffstat (limited to 'volume')
| -rw-r--r-- | volume/volume.go | 28 |
1 files changed, 8 insertions, 20 deletions
diff --git a/volume/volume.go b/volume/volume.go index acec040..48330df 100644 --- a/volume/volume.go +++ b/volume/volume.go @@ -5,13 +5,13 @@ import ( "fmt" ) -type unit float32 +type Volume float32 const ( - CubicCentimetre unit = 1 - Litre unit = 1_000 - CubicMetre unit = 1_000_000 - CubicInch unit = 16.38706 + CubicCentimetre Volume = 1 + Litre Volume = 1_000 + CubicMetre Volume = 1_000_000 + CubicInch Volume = 16.38706 ) // UnitStrings returns a slice of strings, each representing a @@ -22,12 +22,12 @@ func UnitStrings() []string { } const ( - DefaultUnit unit = CubicCentimetre + DefaultUnit Volume = CubicCentimetre // DefaulUnitIndex is used to index UnitStrings(). DefaultUnitIndex int32 = 0 // cc ) -func UnitFromString(s string) (unit, error) { +func UnitFromString(s string) (Volume, error) { // Each case corresponds to a value in UnitStrings(). switch s { case "cc": @@ -39,18 +39,6 @@ func UnitFromString(s string) (unit, error) { case "in³": return CubicInch, nil default: - return *new(unit), errors.New(fmt.Sprintf("invalid volume unit: '%s'", s)) + return *new(Volume), errors.New(fmt.Sprintf("invalid volume unit: '%s'", s)) } } - -type Volume struct { - val float32 -} - -func New(i float32, u unit) Volume { - return Volume{i * float32(u)} -} - -func (v Volume) AsUnit(u unit) float32 { - return v.val / float32(u) -} |