bytesize

package module
v0.0.0-...-40b68ac Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2017 License: MIT Imports: 2 Imported by: 5

README

Go Bytesize

MIT License Go Report Card Go Doc

Overview

Go Bytesize is a utility package for working with common multiples of bytes in Go, such as: Bytes (B), Kilobytes (KB), Megabytes (MB), Gigabytes (GB), Terabytes (TB), Petabytes (PB), Exabytes (EB), Kibibytes (Kib), Mebibytes (MiB), Gibibytes (GiB), Tebibytes (TiB), Pebibytes (PiB), and Exbibytes (EiB).

Example

package main

import (
        "fmt"
        "github.com/zpatrick/go-bytesize"
)

func main() {
        b := bytesize.Bytesize(10000)
        fmt.Printf("%g bytes is: %g KB and %g MB\n", b, b.Kilobytes(), b.Megabytes())

        b = bytesize.TB * 2
        fmt.Printf("2 Terabytes is %g Gibibytes\n", b.Gibibytes())

        b = bytesize.Bytesize(1000000)
        fmt.Printf("%g bytes is %s\n", b, b.Format("mb"))
}

Output:

10000 bytes is: 10 KB and 0.01 MB
2 Terabytes is 1862.645149230957 Gibibytes
1e+06 bytes is 1MB

License

This work is published under the MIT license. Please see the LICENSE file for details.

Documentation

Overview

Example
b := Bytesize(10000)
fmt.Printf("%g bytes is: %g KB and %g MB\n", b, b.Kilobytes(), b.Megabytes())
Output:

10000 bytes is: 10 KB and 0.01 MB

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bytesize

type Bytesize float64

A Bytesize represents a single byte as a float64

const (
	B  Bytesize = 1
	KB Bytesize = 1000 * B
	MB Bytesize = 1000 * KB
	GB Bytesize = 1000 * MB
	TB Bytesize = 1000 * GB
	PB Bytesize = 1000 * TB
	EB Bytesize = 1000 * PB

	KiB Bytesize = 1024 * B
	MiB Bytesize = 1024 * KiB
	GiB Bytesize = 1024 * MiB
	TiB Bytesize = 1024 * GiB
	PiB Bytesize = 1024 * TiB
	EiB Bytesize = 1024 * PiB
)

bytes sizes generally used for computer storage and memory

func (Bytesize) Bytes

func (b Bytesize) Bytes() float64

Bytes returns the number of Bytes (B) in b

func (Bytesize) Exabytes

func (b Bytesize) Exabytes() float64

Exabytes returns the number of Exabytes (EB) in b

func (Bytesize) Exbibytes

func (b Bytesize) Exbibytes() float64

Exbibytes returns the number of Exbibytes (EiB) in b

func (Bytesize) Format

func (b Bytesize) Format(layout string) string

Format returns a textual representation of the Bytesize value formatted according to layout, which defines the format by specifying an abbreviation. Abbreviations are not case-sensitive.

Valid abbreviations are as follows:

B (Bytesizes)
KB (Kilobytes)
MB (Megabytes)
GB (Gigabytes)
TB (Terabytes)
PB (Petabytes)
EB (Exabytes)
KiB (Kibibytes)
MiB (Mebibytes)
GiB (Gibibytes)
TiB (Tebibtyes)
PiB (Pebibytes)
EiB (Exbibyte)
Example
b := Bytesize(100000)
fmt.Println(b.Format("b"))
fmt.Println(b.Format("kb"))
fmt.Println(b.Format("gb"))
fmt.Println(b.Format("tb"))
fmt.Println(b.Format("pb"))
fmt.Println(b.Format("eb"))
fmt.Println(b.Format("kib"))
fmt.Println(b.Format("gib"))
fmt.Println(b.Format("tib"))
fmt.Println(b.Format("pib"))
fmt.Println(b.Format("eib"))
Output:

100000B
100KB
0.0001GB
1e-07TB
1e-10PB
1e-13EB
97.65625KiB
9.313225746154785e-05GiB
9.094947017729282e-08TiB
8.881784197001252e-11PiB
8.673617379884035e-14EiB

func (Bytesize) Gibibytes

func (b Bytesize) Gibibytes() float64

Gibibytes returns the number of Gibibytes (GiB) in b

func (Bytesize) Gigabytes

func (b Bytesize) Gigabytes() float64

Gigabytes returns the number of Gigabytes (GB) in b

func (Bytesize) Kibibytes

func (b Bytesize) Kibibytes() float64

Kibibytes returns the number of Kibibytes (KiB) in b

func (Bytesize) Kilobytes

func (b Bytesize) Kilobytes() float64

Kilobytes returns the number of Kilobytes (KB) in b

func (Bytesize) Mebibytes

func (b Bytesize) Mebibytes() float64

Mebibytes returns the number of Mebibbytes (MiB) in b

func (Bytesize) Megabytes

func (b Bytesize) Megabytes() float64

Megabytes returns the number of Megabytes (MB) in b

func (Bytesize) Pebibytes

func (b Bytesize) Pebibytes() float64

Pebibytes returns the number of Pebibytes (PiB) in b

func (Bytesize) Petabytes

func (b Bytesize) Petabytes() float64

Petabytes returns the number of Petabytes (PB) in b

func (Bytesize) Tebibytes

func (b Bytesize) Tebibytes() float64

Tebibytes returns the number of Tebibytes (TiB) in b

func (Bytesize) Terabytes

func (b Bytesize) Terabytes() float64

Terabytes returns the number of Terabytes (TB) in b

Jump to

Keyboard shortcuts

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