-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpressure.go
66 lines (51 loc) · 1.5 KB
/
pressure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package unit
import (
"encoding"
"fmt"
)
// Pressure is the force applied perpendicular to the surface of an object per unit area over which that
// force is distributed.
type Pressure float64
var _ encoding.TextUnmarshaler = (*Pressure)(nil)
// Pascal is the SI unit for measuring Pressure.
const Pascal Pressure = 1
const pascalSymbol = "Pa"
// KiloPascal is 1000 times the SI unit Pascal.
const KiloPascal Pressure = 1000 * Pascal
const kiloPascalSymbol = "kPa"
// KiloPascal returns p with the unit of kPa.
func (p Pressure) KiloPascal() float64 {
return p.Get(KiloPascal)
}
// Bar is 100'000 times the SI unit Pascal.
const Bar Pressure = 1e5 * Pascal
const barSymbol = "bar"
// Bar returns p with the unit of 1 bar.
func (p Pressure) Bar() float64 {
return p.Get(Bar)
}
// Get returns p with the unit of as.
func (p Pressure) Get(as Pressure) float64 {
return float64(p) / float64(as)
}
// String implements fmt.Stringer.
func (p Pressure) String() string {
return format(float64(p), pascalSymbol)
}
// UnmarshalString sets *p from s.
func (p *Pressure) UnmarshalString(s string) error {
parsed, err := parse(s, map[string]float64{
pascalSymbol: float64(Pascal),
kiloPascalSymbol: float64(KiloPascal),
barSymbol: float64(Bar),
})
if err != nil {
return fmt.Errorf("unmarshal pressure: %w", err)
}
*p = Pressure(parsed)
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (p *Pressure) UnmarshalText(text []byte) error {
return p.UnmarshalString(string(text))
}