size

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: MIT Imports: 6 Imported by: 5

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Size

type Size struct {
	Data float64
	Unit Unit
}

Size represents a bytes size in Unit

func Lookup added in v1.1.0

func Lookup(str string) (Size, bool)

Lookup returns a new size from the given string, return false if parse failed, use B unit if str has no unit string.

func LookupTo added in v1.1.0

func LookupTo(str string, to Unit) (Size, bool)

LookupTo returns a new size from the given string that convert to given unit, return false if failed

func New added in v1.1.0

func New(size float64, unit Unit) Size

New returns a new size

func NewInt added in v1.1.0

func NewInt(size int, unit Unit) Size

func (Size) Round added in v1.1.0

func (s Size) Round(n int32) float64

func (Size) String added in v1.1.0

func (s Size) String() string

String By default, only three decimal places are retained

func (Size) StringRound added in v1.1.0

func (s Size) StringRound(n int32) string

StringRound returns the string representation of the size if n < 0, use the original data as the float representation.

func (Size) To added in v1.1.0

func (s Size) To(to Unit) Size

type Unit added in v1.1.0

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

func (Unit) String added in v1.1.0

func (u Unit) String() string

Jump to

Keyboard shortcuts

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