Skip to content

Golang package to easily and transparently calculate byte sizes

License

Notifications You must be signed in to change notification settings

andreaswachs/sizes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unit tests Go Coverage pkg.go.dev

Sizes

sizes is a Golang package that attempts to produce byte sizes that are obvious for the reader, instead of having nondescript calculations and depending on developers to comment what they were calculating.

Caveats

Overflow risks: If you're computing the byte size of many exabytes, you might run into overflow issues, since the calculations return uint64 and might run out of space for the real number.

Wrong use case: If you're doing many calculations where you compute some byte sizes, it might be faster for you to do the calculations by hand, as a builder pattern is used here and will create a struct each time you calculate a size.

Examples

Calculating 100 megabytes in bytes:

sizes.Megabytes(100)

Bytes are the default unit that the sizes are returned in. If you wish to calculate 100 megabytes in kilobytes:

sizes.MegabytesAs(100, sizes.Kilobyte)

You can also go the other way, calculating from lower units to larger.

Calculating 2 million bytes as kilobytes (please be aware of rounding down since dealing with integers):

sizes.BytesAs(2_000_000, sizes.Megabyte)

About

Golang package to easily and transparently calculate byte sizes

Resources

License

Stars

Watchers

Forks

Languages