Install and import go-humanize
mastergo get and import it using the package path github.com/dustin/go-humanize. The package is accessible via the humanize identifier.repository·master·Indexed 26 days ago
https://github.com/dustin/go-humanizeA Go library for converting raw numbers, sizes, and times into human-readable strings. It provides utilities for formatting byte counts (SI and IEC), adding comma separators to integers and floats, converting timestamps to relative time strings (e.g., '3 days ago'), and generating ordinal strings. Additionally, it includes a subpackage for English pluralization and word series formatting.
go get and import it using the package path github.com/dustin/go-humanize. The package is accessible via the humanize identifier.Use humanize.Comma(n) to format an integer with comma separators (e.g., 1000 becomes '1,000').
fmt.Printf("You owe $%s.\n", humanize.Comma(6582491)) // You owe $6,582,491.Use humanize.Ftoa(f) to format a float64 as a string while removing unnecessary trailing zeros.
fmt.Printf("%s", humanize.Ftoa(2.24)) // 2.24
fmt.Printf("%s", humanize.Ftoa(2.0)) // 2The humanize/english subpackage provides functions to format lists of words with conjunctions:
WordSeries(words, conjunction): Formats a slice of strings with a conjunction (e.g., 'foo, bar and baz').OxfordWordSeries(words, conjunction): Formats a slice of strings using the Oxford comma (e.g., 'foo, bar, and baz').// Using the english subpackage
english.WordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar and baz
english.OxfordWordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar, and bazUse humanize.SI(n, unit) to format a number using SI (metric) notation with a specified unit suffix.
humanize.SI(0.00000000223, "M") // 2.23 nMUse humanize.Time(t) to convert a time.Time instance into a relative time string, such as '12 seconds ago' or '3 days from now'.
fmt.Printf("This was touched %s.", humanize.Time(someTimeInstance)) // This was touched 7 hours ago.Use humanize.Bytes(n) to convert a number of bytes into a human-readable string (e.g., '83 MB' or '79 MiB').
fmt.Printf("That file is %s.", humanize.Bytes(82854982)) // That file is 83 MB.Use humanize.Ordinal(n) to convert an integer into its ordinal string representation (e.g., 1 becomes '1st', 193 becomes '193rd').
fmt.Printf("You're my %s best friend.", humanize.Ordinal(193)) // You are my 193rd best friend.The humanize/english subpackage provides functions for English pluralization:
PluralWord(n, word, irregular): Returns the plural form of a word. If an irregular plural is provided, it is used.Plural(n, word, irregular): Returns a string combining the count and the plural word (e.g., '42 objects').// Using the english subpackage
english.PluralWord(42, "object", "") // objects
english.PluralWord(99, "locus", "loci") // loci
english.Plural(42, "object", "") // 42 objects
english.Plural(99, "locus", "loci") // 99 lociComputeSI to find the most appropriate SI prefix for a given number. It returns the adjusted value (scaled to the prefix) and the corresponding SI prefix string. For example, passing 2.2345e-12 returns 2.2345 and `FormatInteger to convert an int into a formatted string. It accepts a format string (the same syntax used by FormatFloat) and returns the formatted representation. This is a convenience wrapper around FormatFloat designed for use in templates.Use ParseBytes to convert a string representation of bytes (e.g., "42 MB" or "42 mib") into a uint64 value. The function supports both SI and IEC suffixes and handles commas as thousands separators.
Returns the parsed uint64 and an error if the string is invalid or the size is too large for a uint64.