bytesizer

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 4 Imported by: 0

README

bytesizer

bytesizer is a Go package that provides a simple and intuitive way to work with byte counts. It allows you to easily convert between different units of byte sizes, such as bytes, kilobytes, megabytes, gigabytes, terabytes, and petabytes. Similar to how time.Duration works in Go, bytesizer helps in representing byte sizes with appropriate types and methods.

Features

  • Conversion between byte units and bytes
  • Formatting of byte sizes into human-readable strings
  • Parsing of strings into byte sizes with support for different units
  • Fetching byte sizes as integer or floating-point numbers for precision work

Installation

To install bytesizer, you can use the following Go command:

go get github.com/iamlongalong/bytesizer

Replace iamlongalong with your actual GitHub username where the package is hosted.

Usage

Below you'll find the methods provided by the bytesizer package and some examples of how to use them.

Constants

bytesizer defines the following byte size units:

const (
	Byte ByteSize = 1 << (10 * iota)
	KB
	MB
	GB
	TB
	PB
)
Methods
Calc

Calculate the length of a byte slice and return it as a ByteSize:

size := bytesizer.Calc([]byte("Hello World!"))
Format

Format a ByteSize value to a string according to a specified unit:

formattedSize := size.Format(bytesizer.KB) // returns string like "1KB"
String

Convert a ByteSize to a string with an automatic unit:

sizeString := size.String() // returns string like "11B"
Byte, KB, MB, GB, TB, PB

Get the byte size as different units (returns float64):

bytes := size.Byte()
kilobytes := size.KB()
// ... and so on for MB, GB, TB, PB
ByteInt, KBInt, MBInt, GBInt, TBInt, PBInt

Get the byte size as different units (returns int):

bytesInt := size.ByteInt()
kilobytesInt := size.KBInt()
// ... and so on for MBInt, GBInt, TBInt, PBInt
Parse

Parse a string representation of a byte size into a ByteSize object:

size, err := bytesizer.Parse("10KB")
if err != nil {
    log.Fatal(err)
}
fmt.Println(size) // Output: 10240 (Bytes equivalent of 10KB)

Contributing

Contributions to bytesizer are welcome! Feel free to report issues or submit pull requests on our GitHub repository.

License

This package is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteSize

type ByteSize int
const (
	Byte ByteSize = 1 << (10 * iota)
	KB
	MB
	GB
	TB
	PB
)

func Calc

func Calc(b []byte) ByteSize

Calc calc the []byte length, trans to ByteSize.

func Parse

func Parse(s string) (ByteSize, error)

parse a string s in bytes, kilobytes, megabytes, gigabytes, terabytes or petabytes format and converts it into ByteSize, a datatype representing byte sizes. accepts a string s like "10B", "10KB", "10MB", "10GB", "10TB", "10PB" and returns the corresponding ByteSize. returns an error if the format of s is invalid or if an invalid size unit is found.

Example usage:

size, err := Parse("10KB")
if err != nil {
    log.Fatal(err)
}
fmt.Println(size)

Output: 10240 // Bytes equivalent of 10KB

func (ByteSize) Byte

func (fs ByteSize) Byte() float64

Byte method returns the ByteSize in bytes as a float64.

func (ByteSize) ByteInt

func (fs ByteSize) ByteInt() int

ByteInt method returns the ByteSize in bytes as an integer.

func (ByteSize) Format

func (fs ByteSize) Format(bu ByteSize) string

Format method formats the ByteSize value to a string based on the given byte unit. It iterates over the predefined units until it matches the given unit, then calls formatString to generate the final formatted string. If the unit doesn't match any predefined units, it returns the string representation of the ByteSize itself.

func (ByteSize) GB

func (fs ByteSize) GB() float64

GB method returns the ByteSize in gigabytes as a float64.

func (ByteSize) GBInt

func (fs ByteSize) GBInt() int

GBInt method returns the ByteSize in gigabytes as an integer.

func (ByteSize) KB

func (fs ByteSize) KB() float64

KB method returns the ByteSize in kilobytes as a float64.

func (ByteSize) KBInt

func (fs ByteSize) KBInt() int

KBInt method returns the ByteSize in kilobytes as an integer.

func (ByteSize) MB

func (fs ByteSize) MB() float64

MB method returns the ByteSize in megabytes as a float64.

func (ByteSize) MBInt

func (fs ByteSize) MBInt() int

MBInt method returns the ByteSize in megabytes as an integer.

func (ByteSize) PB

func (fs ByteSize) PB() float64

PB method returns the ByteSize in petabytes as a float64.

func (ByteSize) PBInt

func (fs ByteSize) PBInt() int

PBInt method returns the ByteSize in petabytes as an integer.

func (ByteSize) String

func (fs ByteSize) String() string

String method converts ByteSize to a string with an appropriate unit.

func (ByteSize) TB

func (fs ByteSize) TB() float64

TB method returns the ByteSize in terabytes as a float64.

func (ByteSize) TBInt

func (fs ByteSize) TBInt() int

TBInt method returns the ByteSize in terabytes as an integer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL