-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b70818
commit 245837b
Showing
1 changed file
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,67 @@ | ||
# A library for parsing a human-readable format for recording data dimensions. | ||
This library parses a human-readable format for recording data dimensions in the number of bytes. | ||
|
||
# Humansize | ||
This library parses a human-readable format for writing data measurements in byte counts, or turns a byte count into a data measurement format string. | ||
## Installation | ||
``` | ||
go get github.com/MyBlackJay/humansize | ||
``` | ||
|
||
## Example | ||
[Usage](https://go.dev/play/p/pux9fwKARrG) | ||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/MyBlackJay/humansize" | ||
) | ||
|
||
func formatAndPrintKB() { | ||
size := "100KB" | ||
|
||
if parsing, err := humansize.Compile(size); err == nil { | ||
fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64()) | ||
} | ||
} | ||
|
||
func formatAndPrintMiB() { | ||
size := "1MiB" | ||
|
||
if parsing, err := humansize.Compile(size); err == nil { | ||
fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64()) | ||
} | ||
} | ||
|
||
func MustCompileMiWithError() { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
fmt.Println(err) | ||
} | ||
}() | ||
|
||
size := "1MR" | ||
|
||
parsing := humansize.MustCompile(size) | ||
fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64()) | ||
} | ||
|
||
func validateMeasureAndPrint() { | ||
measure := "EiR" | ||
fmt.Println(humansize.ValidateMeasure(measure)) | ||
} | ||
|
||
func TurnBytesIntoToSizeAndPrint() { | ||
size := 2.596 * float64(1<<60) | ||
if res, err := humansize.BytesToSize(size, 10); err == nil { | ||
fmt.Println(res) | ||
} | ||
} | ||
|
||
func main() { | ||
formatAndPrintKB() // 100KB 1024 102400 | ||
formatAndPrintMiB() // 1MiB 1048576 1048576 | ||
MustCompileMiWithError() // unsupported data size format | ||
validateMeasureAndPrint() // false | ||
TurnBytesIntoToSizeAndPrint() // 2.5960000000EB | ||
} | ||
``` | ||
|