Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/tunabay/go-infounit

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/tunabay/go-infounit

  • v1.1.3
  • Source
  • Go
  • Socket score

Version published
Created
Source

go-infounit

Go Reference MIT License

Overview

go-infounit is a Go package providing three data types for units of information:

  • ByteCount - number of bytes with both SI and binary prefixes
  • BitCount - number of bits with both SI and binary prefixes
  • BitRate - number of bits transferred or processed per unit of time

These values can be converted to human-readable string representations using the standard fmt.Printf family functions:

import (
	"fmt"
	"github.com/tunabay/go-infounit"
)

func main() {
	size := infounit.Megabyte * 2500
	fmt.Printf("% s\n", size)    // "2.5 GB"
	fmt.Printf("% .1S\n", size)  // "2.3 GiB"
	fmt.Printf("%# .2s\n", size) // "2.50 gigabytes"
	fmt.Printf("%# .3S\n", size) // "2.328 gibibytes"
	fmt.Printf("%d\n", size)     // "2500000000"

	bits := infounit.Kilobit * 32
	fmt.Printf("% s\n", bits)    // "32 kbit"
	fmt.Printf("% S\n", bits)    // "31.25 Kibit"
	fmt.Printf("%# .2s\n", bits) // "32.00 kilobits"
	fmt.Printf("%# .3S\n", bits) // "31.250 kibibits"
	fmt.Printf("%d\n", bits)     // "32000"

	rate := infounit.GigabitPerSecond * 123.45
	fmt.Printf("% s\n", rate)    // "123.45 Gbit/s"
	fmt.Printf("% .3S\n", rate)  // "114.972 Gibit/s"
	fmt.Printf("%# .1s\n", rate) // "123.5 gigabits per second"
	fmt.Printf("%# .2S\n", rate) // "114.97 gibibits per second"
	fmt.Printf("% .1a\n", rate)  // "123.5 Gbps"
	fmt.Printf("% .2A\n", rate)  // "114.97 Gibps"
}

Run in Go Playground

  • %s is for SI prefixes and %S is for binary prefixes.
  • %a and %A for BitRate use the non-standard abbreviation "bps".
  • ' '(space) flag puts a space between digits and the unit suffix.
  • '#' flag uses long unit suffixes.

They also implement convenience methods for:

  • Rounding to specified precision.
  • Scanning from human-readable string representation using fmt.Scanf family functions.

For more examples:

See also

License

go-infounit is available under the MIT license. See the LICENSE file for more information.

FAQs

Package last updated on 03 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc