bytesize

package module
v0.0.0-...-df43741 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2015 License: BSD-3-Clause Imports: 5 Imported by: 0

README

ByteSize

This is a fork of https://github.com/inhies/go-bytesize This ByteSize fork uses an uint64 instead of a float64 for storing the byte sizes. As bytes are always natural numbers an integer representation is desirable. You do not have to deal with floating point inaccuracy. The disadvantage is that you are limited to sizes up to 2^64. So this fork does not support ZB and YB sizes. The biggest supported size is 18.4EB.

Bytesize is a package for working with measurements of bytes. Using this package allows you to easily add 100KB to 4928MB and get a nicely formatted string representation of the result.

GoDoc Build Status Coverage Status

Usage

Check the built in documentation for examples using the godoc link above or by running godoc locally.

Documentation

Overview

Package bytesize provides functionality for measuring and formatting byte sizes.

You can also perfom mathmatical operation with ByteSize's and the result will be a valid ByteSize with the correct size suffix.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Use long units, such as "megabytes" instead of "MB".
	LongUnits bool = false

	// String format of bytesize output. The unit of measure will be appended
	// to the end. Uses the same formatting options as the fmt package.
	Format string = "%.2f"
)

Functions

This section is empty.

Types

type ByteSize

type ByteSize uint64

ByteSize represents a number of bytes

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

Byte size size suffixes.

func New

func New(s float64) ByteSize

New returns a new ByteSize type set to s.

Example
b := bytesize.New(1024)
fmt.Printf("%s", b)
Output:

1.00KB
Example (Math)
b1 := bytesize.New(1024)
b2 := bytesize.New(4096)
sum := b1 + b2
fmt.Printf("%s", sum)
Output:

5.00KB

func Parse

func Parse(s string) (ByteSize, error)

Parse parses a byte size string. A byte size string is a number followed by a unit suffix, such as "1024B" or "1 MB". Valid byte units are "B", "KB", "MB", "GB", "TB", "PB" and "EB". You can also use the long format of units, such as "kilobyte" or "kilobytes".

Example
b, _ := bytesize.Parse("1024 GB")
fmt.Printf("%s\n", b)

b, _ = bytesize.Parse("3 petabytes")
fmt.Printf("%s\n", b)

bytesize.LongUnits = true
bytesize.Format = "%.0f "
fmt.Printf("%s\n", b)
Output:

1.00TB
3.00PB
3 petabytes

func (ByteSize) Format

func (b ByteSize) Format(format string, unit string, longUnits bool) string

Returns a string representation of b with the specified formatting and units.

Example

Demonstrates using different output formatting and units.

b := 1 * bytesize.TB // Create a new 1 terabyte ByteSize.
fmt.Printf("%s\n", b)
fmt.Printf("%s\n", b.Format("%.8f ", "petabyte", true))
Output:

1.00TB
0.00097656 petabytes

func (*ByteSize) Get

func (b *ByteSize) Get() interface{}

Satisfy the flag package Getter interface.

func (*ByteSize) Set

func (b *ByteSize) Set(s string) error

Satisfy the flag package Value interface.

func (ByteSize) String

func (b ByteSize) String() string

String returns the string form of b using the package global Format and LongUnits options.

Jump to

Keyboard shortcuts

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