Documentation
¶
Overview ¶
Package humanize formats machine values into human-readable English.
It is English-only, stateless, display-side helpers. It does not do internationalization, calendar math, unit conversion, or protocol fields. Humanized text is for display only; never feed it back into protocols, schedulers, billing, or audit.
API ¶
- Bytes formats with decimal units (kB, MB, GB, ...).
- BinaryBytes formats with IEC binary units (KiB, MiB, GiB, ...).
- Number formats integers with comma separators.
- Percent takes a fraction: Percent(0.333) returns "33.3%".
- Duration shows at most two descending units: "1h 30m", "2d 5h".
- Relative formats a target time relative to a reference time.
- Ordinal adds an English ordinal suffix: "1st", "23rd".
- Count joins a count and an English noun: "1,000 items".
Relative time cutovers ¶
Relative uses approximate buckets, not calendar math. Months are 30 days, years are 365 days, days are 24 hours.
|diff| < 1s just now [1s, 60s) N seconds ago / in N seconds [60s, 60m) N minutes ago / in N minutes [60m, 24h) N hours ago / in N hours [24h, 7d) N days ago / in N days [7d, 30d) N weeks ago / in N weeks [30d, 365d) N months ago / in N months >= 365d N years ago / in N years
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BinaryBytes ¶
BinaryBytes formats b with IEC binary byte units such as "1.5 KiB" and "2 MiB". It preserves the sign and saturates math.MinInt64 to math.MaxInt64 when taking the absolute value.
Example ¶
package main
import (
"fmt"
"github.com/agentable/go-humanize"
)
func main() {
const gibibyte = 1024 * 1024 * 1024
fmt.Println(humanize.BinaryBytes(0))
fmt.Println(humanize.BinaryBytes(1536))
fmt.Println(humanize.BinaryBytes(1048576))
fmt.Println(humanize.BinaryBytes(5 * gibibyte))
}
Output: 0 B 1.5 KiB 1 MiB 5 GiB
func Bytes ¶
Bytes formats b with decimal byte units such as "1.5 kB" and "2 MB". It preserves the sign and saturates math.MinInt64 to math.MaxInt64 when taking the absolute value.
Example ¶
package main
import (
"fmt"
"github.com/agentable/go-humanize"
)
func main() {
const gigabyte = 1000 * 1000 * 1000
fmt.Println(humanize.Bytes(0))
fmt.Println(humanize.Bytes(1500))
fmt.Println(humanize.Bytes(1000000))
fmt.Println(humanize.Bytes(5 * gigabyte))
}
Output: 0 B 1.5 kB 1 MB 5 GB
func Count ¶
Count formats count with Number's comma separators and chooses singular when count is ±1.
Example ¶
package main
import (
"fmt"
"github.com/agentable/go-humanize"
)
func main() {
fmt.Println(humanize.Count(1, "item", "items"))
fmt.Println(humanize.Count(0, "item", "items"))
fmt.Println(humanize.Count(1000, "item", "items"))
fmt.Println(humanize.Count(-1, "person", "people"))
}
Output: 1 item 0 items 1,000 items -1 person
func Duration ¶
Duration formats d with up to two significant units such as "1h 30m" and "2d 5h". It preserves the sign, treats a day as 24 hours, uses ms and µs below a second, and returns "0s" for zero.
Example ¶
package main
import (
"fmt"
"time"
"github.com/agentable/go-humanize"
)
func main() {
fmt.Println(humanize.Duration(0))
fmt.Println(humanize.Duration(500 * time.Millisecond))
fmt.Println(humanize.Duration(90 * time.Minute))
fmt.Println(humanize.Duration(29 * time.Hour))
}
Output: 0s 500ms 1h 30m 1d 5h
func Number ¶
Number formats n as a human-readable English integer with comma separators every three digits, such as "1,234,567" and "-1,000,000".
Example ¶
package main
import (
"fmt"
"github.com/agentable/go-humanize"
)
func main() {
fmt.Println(humanize.Number(0))
fmt.Println(humanize.Number(1234567))
fmt.Println(humanize.Number(-1000000))
fmt.Println(humanize.Number(123456789))
}
Output: 0 1,234,567 -1,000,000 123,456,789
func Ordinal ¶
Ordinal formats n with an English ordinal suffix such as "1st", "23rd", and "-3rd".
Example ¶
package main
import (
"fmt"
"github.com/agentable/go-humanize"
)
func main() {
fmt.Println(humanize.Ordinal(1))
fmt.Println(humanize.Ordinal(2))
fmt.Println(humanize.Ordinal(3))
fmt.Println(humanize.Ordinal(11))
fmt.Println(humanize.Ordinal(21))
fmt.Println(humanize.Ordinal(100))
}
Output: 1st 2nd 3rd 11th 21st 100th
func Percent ¶
Percent formats fraction as a percentage with at most one decimal place. The input is a fraction, so 0.333 renders as "33.3%" and 1 renders as "100%". Values above 1 are not clamped: 1.25 renders as "125%". NaN and infinities render as "NaN%", "+Inf%", and "-Inf%".
Example ¶
package main
import (
"fmt"
"github.com/agentable/go-humanize"
)
func main() {
fmt.Println(humanize.Percent(0.75))
fmt.Println(humanize.Percent(0.333))
fmt.Println(humanize.Percent(1.0 / 3))
fmt.Println(humanize.Percent(0))
}
Output: 75% 33.3% 33.3% 0%
func Relative ¶
Relative formats target relative to ref using approximate 30-day months and 365-day years. Durations under a second return "just now". The output describes target's position relative to ref: target before ref renders as "N units ago", and target after ref renders as "in N units".
Example ¶
package main
import (
"fmt"
"time"
"github.com/agentable/go-humanize"
)
func main() {
ref := time.Date(2024, 2, 24, 12, 0, 0, 0, time.UTC)
fmt.Println(humanize.Relative(ref.Add(-3*24*time.Hour), ref))
fmt.Println(humanize.Relative(ref.Add(-7*24*time.Hour), ref))
fmt.Println(humanize.Relative(ref.Add(-30*24*time.Hour), ref))
fmt.Println(humanize.Relative(ref.Add(2*24*time.Hour), ref))
}
Output: 3 days ago 1 week ago 1 month ago in 2 days
Types ¶
This section is empty.