Skip to main content

data.go

data.go - Overview

  1. Overview This file defines a dataFormatter struct and associated methods to format numerical data values into human-readable strings, especially for data size units like bytes, kilobytes, megabytes, etc. It utilizes the go-humanize library for this purpose.

  2. Detailed Documentation

dataFormatter

  • Purpose: Represents a formatter specifically designed for data sizes.
type dataFormatter struct {
}

NewDataFormatter

  • Purpose: Creates and returns a new instance of dataFormatter as a Formatter interface.
  • Returns: A pointer to a dataFormatter object implementing the Formatter interface.
func NewDataFormatter() Formatter {
return &dataFormatter{}
}

(*dataFormatter) Name

  • Purpose: Returns the name of the formatter, which is "data".
  • Returns: A string representing the name of the formatter ("data").
func (*dataFormatter) Name() string {
return "data"
}

(*dataFormatter) Format

  • Purpose: Formats a floating-point value into a human-readable string based on the provided unit. It handles various data size units (bytes, kilobytes, megabytes, etc.) and uses the go-humanize library to convert them into human-readable formats. If the unit is not recognized, it returns the original value as a string.
  • Parameters:
    • value (float64): The numerical value to format.
    • unit (string): The unit of the value (e.g., "bytes", "kbytes", "mbytes").
  • Returns: A string representing the formatted value with the appropriate unit.
func (f *dataFormatter) Format(value float64, unit string) string {
switch unit {
case "bytes":
return humanize.IBytes(uint64(value))
case "decbytes":
return humanize.Bytes(uint64(value))
case "bits":
return humanize.IBytes(uint64(value * converter.Bit))
case "decbits":
return humanize.Bytes(uint64(value * converter.Bit))
case "kbytes":
return humanize.IBytes(uint64(value * converter.Kibibit))
case "decKbytes", "deckbytes":
return humanize.IBytes(uint64(value * converter.Kilobit))
case "mbytes":
return humanize.IBytes(uint64(value * converter.Mebibit))
case "decMbytes", "decmbytes":
return humanize.Bytes(uint64(value * converter.Megabit))
case "gbytes":
return humanize.IBytes(uint64(value * converter.Gibibit))
case "decGbytes", "decgbytes":
return humanize.Bytes(uint64(value * converter.Gigabit))
case "tbytes":
return humanize.IBytes(uint64(value * converter.Tebibit))
case "decTbytes", "dectbytes":
return humanize.Bytes(uint64(value * converter.Terabit))
case "pbytes":
return humanize.IBytes(uint64(value * converter.Pebibit))
case "decPbytes", "decpbytes":
return humanize.Bytes(uint64(value * converter.Petabit))
}
// When unit is not matched, return the value as it is.
return fmt.Sprintf("%v", value)
}
  1. Code Examples Not applicable.

  2. Clarity and Accuracy The documentation reflects the code accurately.

  3. Markdown & MDX Perfection The markdown syntax is correct.

  4. Edge Cases To Avoid Breaking MDX No issues found.

  5. Getting Started Relevance Include in Getting Started: NO