unit: add additional base SI units
* Angle
* Current
* LuminousIntensity
* Mole
* Temperature
and fix comment punctuation.
MODIFY
unit/generate_unit.go
unit/generate_unit.go
@@ -34,9 +34,14 @@ type Dimension struct {
}
const (
+ AngleName string = "AngleDim"
+ CurrentName string = "CurrentDim"
TimeName string = "TimeDim"
LengthName string = "LengthDim"
+ LuminousIntensityName string = "LuminousIntensityDim"
MassName string = "MassDim"
+ MoleName string = "MoleDim"
+ TemperatureName string = "TemperatureDim"
)
type Constant struct {
@@ -137,6 +142,48 @@ var Prefixes = []Prefix{
}
var Units = []Unit{
+ {
+ Name: "Current",
+ Receiver: "i",
+ PrintString: "A",
+ Suffix: "Amper",
+ Singular: "Amper",
+ TypeComment: "Current represents a current in Ampers",
+ Dimensions: []Dimension{
+ {
+ Name: CurrentName,
+ Power: 1,
+ },
+ },
+ },
+ {
+ Name: "Angle",
+ Receiver: "a",
+ PrintString: "rad",
+ Suffix: "rad",
+ Singular: "Rad",
+ TypeComment: "Angle represents an angle in radians",
+ Dimensions: []Dimension{
+ {
+ Name: AngleName,
+ Power: 1,
+ },
+ },
+ },
+ {
+ Name: "LuminousIntensity",
+ Receiver: "j",
+ PrintString: "cd",
+ Suffix: "candela",
+ Singular: "Candela",
+ TypeComment: "Candela represents a luminous intensity in candela",
+ Dimensions: []Dimension{
+ {
+ Name: LuminousIntensityName,
+ Power: 1,
+ },
+ },
+ },
{
Name: "Mass",
Receiver: "m",
@@ -152,6 +199,20 @@ var Units = []Unit{
},
},
},
+ {
+ Name: "Mole",
+ Receiver: "n",
+ PrintString: "mol",
+ Suffix: "mol",
+ Singular: "mol",
+ TypeComment: "Mole represents an amount in moles",
+ Dimensions: []Dimension{
+ {
+ Name: MoleName,
+ Power: 1,
+ },
+ },
+ },
{
Name: "Length",
Receiver: "l",
@@ -166,6 +227,21 @@ var Units = []Unit{
},
},
},
+ {
+ Name: "Temperature",
+ Receiver: "t",
+ PrintString: "K",
+ Suffix: "Kelvin",
+ Singular: "Kelvin",
+ TypeComment: "Temperature represents a temperature in Kelvin",
+ Dimensions: []Dimension{
+ {
+ Name: TemperatureName,
+ Power: 1,
+ },
+ },
+ ErForm: "Temperaturer",
+ },
{
Name: "Time",
Receiver: "t",
@@ -214,7 +290,7 @@ import (
"math"
)
-// {{.TypeComment}}
+// {{.TypeComment}}.
type {{.Name}} float64
`

MODIFY
unit/length.go
unit/length.go
@@ -12,7 +12,7 @@ import (
"math"
)
-// Length represents a length in meters
+// Length represents a length in meters.
type Length float64
const (

MODIFY
unit/mass.go
unit/mass.go
@@ -12,7 +12,7 @@ import (
"math"
)
-// Mass represents a mass in kilograms
+// Mass represents a mass in kilograms.
type Mass float64
const (

MODIFY
unit/time.go
unit/time.go
@@ -12,7 +12,7 @@ import (
"math"
)
-// Time represents a time in seconds
+// Time represents a time in seconds.
type Time float64
const (

MODIFY
unit/unit_test.go
unit/unit_test.go
@@ -36,6 +36,27 @@ var formatTests = []struct {
{Dimless(math.E), "%1v", "2.718281828459045"},
{Dimless(math.E), "%#v", "unit.Dimless(2.718281828459045)"},
{Dimless(math.E), "%s", "%!s(unit.Dimless=2.718281828459045)"},
+ {Angle(1), "%v", "1 rad"},
+ {Angle(0.45), "%.1v", "0.5 rad"},
+ {Angle(0.45), "%10.1v", " 0.5 rad"},
+ {Angle(0.45), "%10v", " 0.45 rad"},
+ {Angle(0.45), "%1v", "0.45 rad"},
+ {Angle(1), "%#v", "unit.Angle(1)"},
+ {Angle(1), "%s", "%!s(unit.Angle=1 rad)"},
+ {Current(1), "%v", "1 A"},
+ {Current(0.45), "%.1v", "0.5 A"},
+ {Current(0.45), "%10.1v", " 0.5 A"},
+ {Current(0.45), "%10v", " 0.45 A"},
+ {Current(0.45), "%1v", "0.45 A"},
+ {Current(1), "%#v", "unit.Current(1)"},
+ {Current(1), "%s", "%!s(unit.Current=1 A)"},
+ {LuminousIntensity(1), "%v", "1 cd"},
+ {LuminousIntensity(0.45), "%.1v", "0.5 cd"},
+ {LuminousIntensity(0.45), "%10.1v", " 0.5 cd"},
+ {LuminousIntensity(0.45), "%10v", " 0.45 cd"},
+ {LuminousIntensity(0.45), "%1v", "0.45 cd"},
+ {LuminousIntensity(1), "%#v", "unit.LuminousIntensity(1)"},
+ {LuminousIntensity(1), "%s", "%!s(unit.LuminousIntensity=1 cd)"},
{Mass(1), "%v", "1 kg"},
{Mass(0.45), "%.1v", "0.5 kg"},
{Mass(0.45), "%10.1v", " 0.5 kg"},
@@ -43,6 +64,13 @@ var formatTests = []struct {
{Mass(0.45), "%1v", "0.45 kg"},
{Mass(1), "%#v", "unit.Mass(1)"},
{Mass(1), "%s", "%!s(unit.Mass=1 kg)"},
+ {Mole(1), "%v", "1 mol"},
+ {Mole(0.45), "%.1v", "0.5 mol"},
+ {Mole(0.45), "%10.1v", " 0.5 mol"},
+ {Mole(0.45), "%10v", " 0.45 mol"},
+ {Mole(0.45), "%1v", "0.45 mol"},
+ {Mole(1), "%#v", "unit.Mole(1)"},
+ {Mole(1), "%s", "%!s(unit.Mole=1 mol)"},
{Length(1.61619926e-35), "%v", "1.61619926e-35 m"},
{Length(1.61619926e-35), "%.2v", "1.6e-35 m"},
{Length(1.61619926e-35), "%10.2v", " 1.6e-35 m"},
@@ -50,6 +78,13 @@ var formatTests = []struct {
{Length(1.61619926e-35), "%1v", "1.61619926e-35 m"},
{Length(1.61619926e-35), "%#v", "unit.Length(1.61619926e-35)"},
{Length(1.61619926e-35), "%s", "%!s(unit.Length=1.61619926e-35 m)"},
+ {Temperature(15.2), "%v", "15.2 K"},
+ {Temperature(15.2), "%.2v", "15 K"},
+ {Temperature(15.2), "%6.2v", " 15 K"},
+ {Temperature(15.2), "%10v", " 15.2 K"},
+ {Temperature(15.2), "%1v", "15.2 K"},
+ {Temperature(15.2), "%#v", "unit.Temperature(15.2)"},
+ {Temperature(15.2), "%s", "%!s(unit.Temperature=15.2 K)"},
{Time(15.2), "%v", "15.2 s"},
{Time(15.2), "%.2v", "15 s"},
{Time(15.2), "%6.2v", " 15 s"},
@@ -68,8 +103,8 @@ func TestFormat(t *testing.T) {
}
func TestGoStringFormat(t *testing.T) {
- expect1 := `&unit.Unit{dimensions:unit.Dimensions{4:2, 6:-1}, formatted:"", value:6.62606957e-34}`
- expect2 := `&unit.Unit{dimensions:unit.Dimensions{6:-1, 4:2}, formatted:"", value:6.62606957e-34}`
+ expect1 := `&unit.Unit{dimensions:unit.Dimensions{4:2, 7:-1}, formatted:"", value:6.62606957e-34}`
+ expect2 := `&unit.Unit{dimensions:unit.Dimensions{7:-1, 4:2}, formatted:"", value:6.62606957e-34}`
if r := fmt.Sprintf("%#v", New(6.62606957e-34, Dimensions{MassDim: 2, TimeDim: -1})); r != expect1 && r != expect2 {
t.Errorf("Format %q: got: %q expected: %q", "%#v", r, expect1)
}

MODIFY
unit/unittype.go
unit/unittype.go
@@ -39,6 +39,7 @@ const (
LengthDim
LuminousIntensityDim
MassDim
+ MoleDim
TemperatureDim
TimeDim
// Other common SI Dimensions
@@ -51,6 +52,7 @@ var (
LengthDim: "m",
LuminousIntensityDim: "cd",
MassDim: "kg",
+ MoleDim: "mol",
TemperatureDim: "K",
TimeDim: "s",
AngleDim: "rad",
@@ -63,13 +65,12 @@ var (
"m": LengthDim,
"cd": LuminousIntensityDim,
"kg": MassDim,
+ "mol": MoleDim,
"K": TemperatureDim,
"s": TimeDim,
"rad": AngleDim,
// Reserve common SI symbols
- // base units
- "mol": reserved,
// prefixes
"Y": reserved,
"Z": reserved,

unit: add additional base SI units

* Angle
* Current
* LuminousIntensity
* Mole
* Temperature

and fix comment punctuation.