xunits

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 9 Imported by: 0

README

xunits

Custom types for managing units of magnitude: bytes, durations, and other common measurements.

 

xunits aggregates reusable types that either create new magnitude representations (like Bytes for human-readable data sizes) or extend existing Go types (like TimeDuration which wraps time.Duration with additional features).

The package is designed to provide convenient, JSON-friendly types for CLI tools, configuration management, and service applications.


Purpose

xunits centralizes custom types for common units of measurement, with the goal of:

  • creating new types that don't exist in the standard library (Bytes),
  • extending existing types with additional capabilities (TimeDuration),
  • providing JSON marshaling/unmarshaling support for these types,
  • making it easy to work with human-readable representations.

This is particularly useful when accepting configuration, command-line flags, or API inputs that need to support friendly formats like "1.5GB" or "7d".


Installation

Add the package to your module:

  go get github.com/AeonDigital/Go-Core/xunits@latest

Import it in your code:

import "github.com/AeonDigital/Go-Core/xunits"

Basic usage

Bytes

Work with data sizes using human-readable notation:

var size xunits.Bytes
json.Unmarshal([]byte(`"1.5GB"`), &size)
fmt.Println(size)  // Output: "1.50GB"
fmt.Println(size.String())  // Output: "1.50GB"

// Access constants
allocSize := 10 * xunits.MB

The Bytes type supports JSON input like "10mb", "500K", "1.5GB", and formats output with two decimal places for readability.

TimeDuration

Parse and work with durations including day notation:

var duration xunits.TimeDuration
json.Unmarshal([]byte(`"7d"`), &duration)
fmt.Println(duration.String())  // Output: "7d" (since it's exactly 7 days)

var mixed xunits.TimeDuration
json.Unmarshal([]byte(`"1h30m"`), &mixed)
fmt.Println(mixed.String())  // Output: "1h30m0s" (not a day multiple)

TimeDuration extends the standard time.Duration with:

  • JSON marshaling support,
  • parsing of the custom "d" suffix for days,
  • automatic formatting back to "Xd" notation for day multiples.

Supported APIs

The package exposes two main types:

Bytes
  • xunits.Bytes — unsigned 64-bit integer representing data size.
  • xunits.Bytes.UnmarshalJSON([]byte) error — parse JSON strings like "10mb".
  • xunits.Bytes.String() string — format to human-readable representation.
  • Constants: xunits.B, xunits.KB, xunits.MB, xunits.GB, xunits.TB.
TimeDuration
  • xunits.TimeDuration — wraps time.Duration with custom marshaling.
  • xunits.TimeDuration.MarshalJSON() ([]byte, error) — serialize to JSON string.
  • xunits.TimeDuration.UnmarshalJSON([]byte) error — parse JSON including "Xd" syntax.
  • xunits.TimeDuration.String() string — format with day notation when applicable.

External dependencies

xunits depends only on the Go standard library:

  • encoding/json
  • fmt
  • strconv
  • strings
  • time
  • unicode

It also depends on the internal repository package:

  • github.com/AeonDigital/Go-Core/xerrors

No third-party measurement or time libraries are required.

Documentation

Index

Constants

View Source
const (
	B  Bytes = 1
	KB       = B * 1024
	MB       = KB * 1024
	GB       = MB * 1024
	TB       = GB * 1024
)

Binary/IEC byte size constants based on powers of 1024.

View Source
const (
	XERR_NONE   xerrors.ErrorCode = ""
	XERR_PKGCTX xerrors.ErrorCode = "ERR_XUNITS"
)
View Source
const XERR_PKGCTX_BYTES xerrors.ErrorCode = "XUNITS.BYTES"
View Source
const XERR_PKGCTX_TIMEDURATION xerrors.ErrorCode = "XUNITS.TIMEDURATION"

Variables

This section is empty.

Functions

This section is empty.

Types

type Bytes

type Bytes uint64

Bytes represents a data size in bytes as an unsigned 64-bit integer.

func (Bytes) String

func (bs Bytes) String() string

String implements the fmt.Stringer interface. It formats the Bytes value back into a human-readable string rounded to two decimal places (e.g., "1.50MB", "450B").

func (*Bytes) UnmarshalJSON

func (bs *Bytes) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It parses a JSON string containing human-readable data sizes (e.g., "10mb", "500K", "1.5GB") into a Bytes integer value.

type TimeDuration

type TimeDuration struct {
	time.Duration
}

TimeDuration extends standard time.Duration to add custom JSON serialization and support for day-based representations (e.g., "7d").

func (TimeDuration) MarshalJSON

func (o TimeDuration) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It serializes the TimeDuration into its string representation.

func (TimeDuration) String

func (o TimeDuration) String() string

String returns a string representation of the duration. If the duration is an exact multiple of 24 hours, it formats it using the "d" suffix (e.g., "2d"). Otherwise, it falls back to time.Duration.String().

func (*TimeDuration) UnmarshalJSON

func (o *TimeDuration) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It parses a JSON string into a TimeDuration, extending time.ParseDuration to natively support the "d" suffix for days (1d = 24h).

Jump to

Keyboard shortcuts

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