Skip to content
/ size Public

Fast conversion between file size and string

License

Notifications You must be signed in to change notification settings

dstgo/size

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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