Skip to content

proposal: x/dataunit: a standard package for parsing and formatting data size units (e.g., "MB", "GiB") #75722

@OhJuhun

Description

@OhJuhun

Proposal Details

Hi,

I'd like to propose adding a standard package (e.g., x/dataunit) to handle parsing and formatting of data size units such as "MB", "KB", "GiB", etc.

Motivation:
Many Go projects need to handle data size expressions in configuration files, environment variables, command-line flags, and user interfaces. Currently, projects are either reimplementing similar parsing logic or depending on third-party modules (like github.com/alecthomas/units, github.com/docker/go-units). Having a standardized package would:

  • Provide reliable parsing (e.g., "256MB" → int64)
  • Support both SI and IEC standards (e.g., 1KB=1000B vs 1KiB=1024B)
  • Enable consistent formatting (e.g., 1536000 → "1.5MB")
  • Encourage best practices and reduce duplicated code across the community

Examples:

dataunit.Parse("2GB") // 2147483648, nil
dataunit.Format(1048576) // "1MB"
dataunit.Parse("512KiB") // 524288, nil

Prior Art:
This feature is popular in other languages and the Go ecosystem:

  • Python: humanfriendly, byteutils
  • Go: github.com/alecthomas/units, github.com/docker/go-units

Proposal:
Introduce a package (possibly x/dataunit) to support parsing strings and formatting integers as data sizes, covering both SI and IEC units.

Would the proposal consider contributions or feedback from the community, or is there a plan for implementation?

Thanks for considering!

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions