/
dataunit.go
51 lines (42 loc) · 1.27 KB
/
dataunit.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
package dataunit
// Size represents a measurement of data and can return that measurement
// in different units (B, KB, MB, GB, TB, PB, EB)
type Size int64
// Bytes returns the size as a uint64 of bytes
func (s Size) Bytes() int64 {
return int64(s)
}
// Kilobytes returns the size as a float64 of kilobytes
func (s Size) Kilobytes() float64 {
return float64(s) / float64(Kilobyte)
}
// Megabytes returns the size as a float64 of kilobytes
func (s Size) Megabytes() float64 {
return float64(s) / float64(Megabyte)
}
// Gigabytes returns the size as a float64 of gigabytes
func (s Size) Gigabytes() float64 {
return float64(s) / float64(Gigabyte)
}
// Terabytes returns the size as a float64 of terabytes
func (s Size) Terabytes() float64 {
return float64(s) / float64(Terabyte)
}
// Petabytes returns the size as a float64 of petabytes
func (s Size) Petabytes() float64 {
return float64(s) / float64(Petabyte)
}
// Exabytes returns the size as a float64 of exabytes
func (s Size) Exabytes() float64 {
return float64(s) / float64(Exabyte)
}
// Common size units
const (
Byte Size = 1
Kilobyte = 1024 * Byte
Megabyte = 1024 * Kilobyte
Gigabyte = 1024 * Megabyte
Terabyte = 1024 * Gigabyte
Petabyte = 1024 * Terabyte
Exabyte = 1024 * Petabyte
)