Skip to content

MyBlackJay/humansize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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 TurnBytesIntoSizeAndPrint() {
	size := 2.596 * float64(1<<60)
	if res, err := humansize.BytesToSize(size, 10); err == nil {
		fmt.Println(res)
	}
}

func TurnBytesIntoSizeInMeasureAndPrint() {
	if res, err := humansize.Compile("2048MB"); err == nil {
		fmt.Println(res.GetCompiledInMeasure("gib"))
	}
}

func main() {
	formatAndPrintKB()                   // 100KB 1024 102400
	formatAndPrintMiB()                  // 1MiB 1048576 1048576
	MustCompileMiWithError()             // unsupported data size format
	validateMeasureAndPrint()            // false
	TurnBytesIntoSizeAndPrint()          // 2.5960000000EB
	TurnBytesIntoSizeInMeasureAndPrint() // 2 <nil>

}