Categorygithub.com/dstgo/size
repositorypackage
1.1.0
Repository: https://github.com/dstgo/size.git
Documentation: pkg.go.dev

# README

size

simple conversion between size and string

install

go get -u github.com/dstgo/size@latest

units

type Unit = int

const (
	B Unit = 1 << (iota * 10)
	KB
	MB
	GB
	TB
	PB
	EB
)

usage

package main

import (
	"fmt"
	"github.com/dstgo/size"
)

func main() {
	s1 := size.NewInt(1, size.KB)
	fmt.Println(s1)
	s2 := s1.To(size.MB)
	fmt.Println(s2)

	s3, ok := size.Lookup("1.2MB")
	if !ok {
		panic("failed to lookup")
	}
	fmt.Println(s3)
	lookupTo, ok := size.LookupTo("1.2MB", size.KB)
	if !ok {
		panic("failed to lookup")
	}
	fmt.Println(lookupTo)
}

output

1KB
0.001MB 
1.2MB   
1228.8KB