initial commit

This commit is contained in:
2025-05-16 14:53:37 +10:00
commit 8e2ca9de19
9 changed files with 430 additions and 0 deletions

53
sizes/bibyte.go Normal file
View File

@@ -0,0 +1,53 @@
package sizes
/*
Copyright 2025 Suyono <suyono3484@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
func (b BiByteUnit) AsByte() ByteUnit {
return ByteUnit(b)
}
func (b BiByteUnit) AsBit() BitUnit {
return BitUnit(b * 8)
}
func (b BiByteUnit) AsBiByte() BiByteUnit {
return b
}
func (b BiByteUnit) Kibi() float64 {
return float64(b / KibiByte)
}
func (b BiByteUnit) Mebi() float64 {
return float64(b / MebiByte)
}
func (b BiByteUnit) Gibi() float64 {
return float64(b / GibiByte)
}
func (b BiByteUnit) Tebi() float64 {
return float64(b / TebiByte)
}
func (b BiByteUnit) Pebi() float64 {
return float64(b / PebiByte)
}
func (b BiByteUnit) Exbi() float64 {
return float64(b / ExbiByte)
}

59
sizes/bit.go Normal file
View File

@@ -0,0 +1,59 @@
package sizes
/*
Copyright 2025 Suyono <suyono3484@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import "math"
func (b BitUnit) AsByte() ByteUnit {
return ByteUnit(math.Ceil(float64(b / 8)))
}
func (b BitUnit) AsBiByte() BiByteUnit {
return BiByteUnit(math.Ceil(float64(b / 8)))
}
func (b BitUnit) AsBit() BitUnit {
return b
}
func (b BitUnit) Kilo() float64 {
return float64(b / KiloBit)
}
func (b BitUnit) Mega() float64 {
return float64(b / MegaBit)
}
func (b BitUnit) Giga() float64 {
return float64(b / GigaBit)
}
func (b BitUnit) Tera() float64 {
return float64(b / TeraBit)
}
func (b BitUnit) Peta() float64 {
return float64(b / PetaBit)
}
func (b BitUnit) Exa() float64 {
return float64(b / ExaBit)
}
func (b BitUnit) Zeta() float64 {
return float64(b / ZetaBit)
}

57
sizes/byte.go Normal file
View File

@@ -0,0 +1,57 @@
package sizes
/*
Copyright 2025 Suyono <suyono3484@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
func (b ByteUnit) AsByte() ByteUnit {
return b
}
func (b ByteUnit) AsBit() BitUnit {
return BitUnit(b * 8)
}
func (b ByteUnit) AsBiByte() BiByteUnit {
return BiByteUnit(b)
}
func (b ByteUnit) Kilo() float64 {
return float64(b / KiloByte)
}
func (b ByteUnit) Mega() float64 {
return float64(b / MegaByte)
}
func (b ByteUnit) Giga() float64 {
return float64(b / GigaByte)
}
func (b ByteUnit) Tera() float64 {
return float64(b / TeraByte)
}
func (b ByteUnit) Peta() float64 {
return float64(b / PetaByte)
}
func (b ByteUnit) Exa() float64 {
return float64(b / ExaByte)
}
func (b ByteUnit) Zeta() float64 {
return float64(b / ZetaByte)
}

55
sizes/sizes.go Normal file
View File

@@ -0,0 +1,55 @@
package sizes
/*
Copyright 2025 Suyono <suyono3484@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
type BitUnit int64
type ByteUnit int64
type BiByteUnit int64
const (
Bit BitUnit = 1
KiloBit BitUnit = 1000
MegaBit BitUnit = 1000000
GigaBit BitUnit = 1000000000
TeraBit BitUnit = 1000000000000
PetaBit BitUnit = 1000000000000000
ExaBit BitUnit = 10000000000000000000
ZetaBit BitUnit = 10000000000000000000000
)
const (
Byte ByteUnit = 1
KiloByte ByteUnit = 1000
MegaByte ByteUnit = 1000000
GigaByte ByteUnit = 1000000000
TeraByte ByteUnit = 1000000000000
PetaByte ByteUnit = 1000000000000000
ExaByte ByteUnit = 10000000000000000000
ZetaByte ByteUnit = 10000000000000000000000
)
const (
BiByte BiByteUnit = 1 << (10 * iota)
KibiByte
MebiByte
GibiByte
TebiByte
PebiByte
ExbiByte
)