Display file sizes as '83 MB' or '79 MiB' instead of raw byte counts in a Go application.
Show relative timestamps like '12 seconds ago' or '3 days from now' in a user-facing interface.
Format large numbers with comma separators and ordinal suffixes for dashboards or reports.
Generate grammatically correct English phrases like '1 object' or '5 objects' from a count.
go-humanize is a small Go library that formats numbers and times into text that reads naturally to a person rather than as raw digits. Programs frequently need to show data to users, and a raw number like 82854982 is harder to read than "83 MB". The library handles these conversions so you do not have to write the formatting logic yourself. The byte size formatter takes a number and returns a string in megabytes, gigabytes, or the appropriate unit, and you can choose between SI units (MB, GB) and binary units (MiB, GiB). The time formatter takes a point in time and returns a phrase like "12 seconds ago" or "3 days from now" relative to the current moment. These two are the most common uses. Beyond sizes and times, the library also handles several other formatting tasks. The ordinal formatter turns a number like 193 into "193rd" with the correct English suffix. The comma formatter inserts thousand separators so 1000000 becomes "1,000,000". A float formatter strips trailing zeros so 2.240000 becomes "2.24". An SI notation formatter expresses very small or very large numbers using metric prefixes like nano or mega. An English-specific subpackage adds two more tools. One handles simple pluralization, returning "object" or "objects" depending on the count, with support for irregular forms. The other formats a list of words into a natural comma-separated series with a conjunction, such as "foo, bar and baz" or the Oxford comma variant "foo, bar, and baz". The library is installed via the standard Go module system and is well documented on pkg.go.dev. It has no external dependencies beyond the Go standard library.
← dustin on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.