### Pluralize English Words with Quantities Source: https://context7.com/dustin/go-humanize/llms.txt Formats an integer with a properly pluralized English word. Handles both regular and irregular plurals. Use `Plural` to include the number, and `PluralWord` to get only the correctly pluralized word. ```go package main import ( "fmt" "github.com/dustin/go-humanize/english" ) func main() { // Basic pluralization fmt.Println(english.Plural(1, "object", "")) // Output: 1 object fmt.Println(english.Plural(42, "object", "")) // Output: 42 objects fmt.Println(english.Plural(2, "bus", "")) // Output: 2 buses // Irregular plurals fmt.Println(english.Plural(99, "locus", "loci")) // Output: 99 loci fmt.Println(english.Plural(1, "matrix", "")) // Output: 1 matrix fmt.Println(english.Plural(5, "matrix", "")) // Output: 5 matrices // Just the word (no number) fmt.Println(english.PluralWord(1, "file", "")) // Output: file fmt.Println(english.PluralWord(10, "file", "")) // Output: files fmt.Println(english.PluralWord(2, "index", "")) // Output: indices } ``` -------------------------------- ### Predefined Byte Size Constants in Go Source: https://context7.com/dustin/go-humanize/llms.txt Utilize predefined constants for SI (base 1000) and IEC (base 1024) byte sizes provided by the go-humanize library. These constants can be used directly in calculations. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { // IEC sizes (base 1024) fmt.Printf("1 KiB = %d bytes\n", humanize.KiByte) // Output: 1 KiB = 1024 bytes fmt.Printf("1 MiB = %d bytes\n", humanize.MiByte) // Output: 1 MiB = 1048576 bytes fmt.Printf("1 GiB = %d bytes\n", humanize.GiByte) // Output: 1 GiB = 1073741824 bytes // SI sizes (base 1000) fmt.Printf("1 KB = %d bytes\n", humanize.KByte) // Output: 1 KB = 1000 bytes fmt.Printf("1 MB = %d bytes\n", humanize.MByte) // Output: 1 MB = 1000000 bytes fmt.Printf("1 GB = %d bytes\n", humanize.GByte) // Output: 1 GB = 1000000000 bytes // Use in calculations fileSize := uint64(5 * humanize.GiByte) fmt.Println(humanize.IBytes(fileSize)) // Output: 5.0 GiB } ``` -------------------------------- ### Format Word Lists with Conjunctions in Go Source: https://context7.com/dustin/go-humanize/llms.txt Use WordSeries and OxfordWordSeries to format slices of strings into grammatically correct lists with specified conjunctions. Supports standard and Oxford comma styles. ```go package main import ( "fmt" "github.com/dustin/go-humanize/english" ) func main() { // Standard word series (no Oxford comma) fmt.Println(english.WordSeries([]string{"foo"}, "and")) // Output: foo fmt.Println(english.WordSeries([]string{"foo", "bar"}, "and")) // Output: foo and bar fmt.Println(english.WordSeries([]string{"foo", "bar", "baz"}, "and")) // Output: foo, bar and baz // Oxford comma style fmt.Println(english.OxfordWordSeries([]string{"foo", "bar", "baz"}, "and")) // Output: foo, bar, and baz // Using "or" conjunction fmt.Println(english.WordSeries([]string{"red", "green", "blue"}, "or")) // Output: red, green or blue fmt.Println(english.OxfordWordSeries([]string{"red", "green", "blue"}, "or")) // Output: red, green, or blue } ``` -------------------------------- ### Format Bytes to Human-Readable SI String Source: https://context7.com/dustin/go-humanize/llms.txt Use `Bytes` for default SI formatting (base 1000) with 2 significant digits. Use `BytesN` to specify custom precision. Both functions format byte counts into strings like '83 MB' or '1.0 kB'. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { // Basic byte formatting (SI units, base 1000) fmt.Println(humanize.Bytes(82854982)) // Output: 83 MB fmt.Println(humanize.Bytes(1000)) // Output: 1.0 kB fmt.Println(humanize.Bytes(1000000000)) // Output: 1.0 GB fmt.Println(humanize.Bytes(5)) // Output: 5 B // BytesN for custom precision fmt.Println(humanize.BytesN(82854982, 3)) // Output: 82.9 MB fmt.Println(humanize.BytesN(82854982, 4)) // Output: 82.85 MB } ``` -------------------------------- ### Format Word Series with Conjunction Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Creates a comma-separated list of words, joining the last two with a specified conjunction. ```go english.WordSeries([]string{"foo"}, "and") // foo english.WordSeries([]string{"foo", "bar"}, "and") // foo and bar english.WordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar and baz ``` -------------------------------- ### Format Bytes to Human-Readable String Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Use this function to convert a byte count into a human-readable string representation (e.g., KB, MB, GB). ```go fmt.Printf("That file is %s.", humanize.Bytes(82854982)) // That file is 83 MB. ``` -------------------------------- ### Format Bytes to Human-Readable IEC String Source: https://context7.com/dustin/go-humanize/llms.txt Use `IBytes` for default IEC binary formatting (base 1024) with 2 significant digits. Use `IBytesN` to specify custom precision. These functions format byte counts into strings like '79 MiB' or '1.0 KiB'. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { // IEC units (base 1024) fmt.Println(humanize.IBytes(82854982)) // Output: 79 MiB fmt.Println(humanize.IBytes(1024)) // Output: 1.0 KiB fmt.Println(humanize.IBytes(1073741824)) // Output: 1.0 GiB // IBytesN for custom precision fmt.Println(humanize.IBytesN(82854982, 4)) // Output: 79.02 MiB fmt.Println(humanize.IBytesN(123456789, 3)) // Output: 118 MiB fmt.Println(humanize.IBytesN(123456789, 6)) // Output: 117.738 MiB } ``` -------------------------------- ### Format Integers with Thousand Separators (Comma) Source: https://context7.com/dustin/go-humanize/llms.txt Use `humanize.Comma` to format integers with commas as thousand separators for better readability. Handles positive, zero, and negative integers. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { fmt.Println(humanize.Comma(0)) fmt.Println(humanize.Comma(100)) fmt.Println(humanize.Comma(1000)) fmt.Println(humanize.Comma(1000000)) fmt.Println(humanize.Comma(1000000000)) fmt.Println(humanize.Comma(-100000)) fmt.Println(humanize.Comma(-1234567890)) } ``` -------------------------------- ### Parse Human-Readable Size String to Bytes Source: https://context7.com/dustin/go-humanize/llms.txt The `ParseBytes` function converts human-readable byte strings (e.g., '42 MB', '1.5 GB', '100 kB') back into their byte count. It supports both SI and IEC units, with or without the 'B' suffix, and handles commas in numbers. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { // Parse SI units bytes, err := humanize.ParseBytes("42 MB") if err != nil { panic(err) } fmt.Println(bytes) // Output: 42000000 // Parse IEC units bytes, err = humanize.ParseBytes("42 MiB") if err != nil { panic(err) } fmt.Println(bytes) // Output: 44040192 // Works with various formats bytes, _ = humanize.ParseBytes("1.5 GB") fmt.Println(bytes) // Output: 1500000000 bytes, _ = humanize.ParseBytes("100 kB") fmt.Println(bytes) // Output: 100000 // Handles commas in numbers bytes, _ = humanize.ParseBytes("1,234 MB") fmt.Println(bytes) // Output: 1234000000 } ``` -------------------------------- ### Format Big Integer Byte Sizes Source: https://context7.com/dustin/go-humanize/llms.txt Formats `math/big.Int` byte values for extremely large sizes, supporting units beyond exabytes. Use `BigBytes` for SI units and `BigIBytes` for IEC units. Also includes a function to parse big byte strings. ```go package main import ( "fmt" "math/big" "github.com/dustin/go-humanize" ) func main() { // Large byte values with SI units bigNum := big.NewInt(82854982) fmt.Println(humanize.BigBytes(bigNum)) // Output: 83 MB // Large byte values with IEC units fmt.Println(humanize.BigIBytes(bigNum)) // Output: 79 MiB // Very large values (zettabytes, yottabytes) huge := new(big.Int) huge.SetString("1000000000000000000000000", 10) // 1 YB fmt.Println(humanize.BigBytes(huge)) // Output: 1.0 YB // Parse big byte strings parsed, err := humanize.ParseBigBytes("1.5 YB") if err != nil { panic(err) } fmt.Println(parsed) // Output: 1500000000000000000000000 } ``` -------------------------------- ### Format Time to Human-Readable String Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Convert a time.Time instance into a relative time string (e.g., '12 seconds ago', '3 days from now'). ```go fmt.Printf("This was touched %s.", humanize.Time(someTimeInstance)) // This was touched 7 hours ago. ``` -------------------------------- ### Format Number with SI Notation Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Formats a number using SI notation (e.g., for scientific or engineering contexts). ```go humanize.SI(0.00000000223, "M") // 2.23 nM ``` -------------------------------- ### Format Numbers with SI Prefixes Source: https://context7.com/dustin/go-humanize/llms.txt Formats numbers using SI notation with metric prefixes. Use this for scientific values or when dealing with large/small numbers that benefit from prefix representation. Ensure the unit is provided. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { // Scientific values with units fmt.Println(humanize.SI(0.00000000223, "M")) // Output: 2.23 nM fmt.Println(humanize.SI(1000000, "B")) // Output: 1 MB fmt.Println(humanize.SI(2.2345e-12, "F")) // Output: 2.2345 pF fmt.Println(humanize.SI(1500, "W")) // Output: 1.5 kW fmt.Println(humanize.SI(0.001, "A")) // Output: 1 mA // With digit limits fmt.Println(humanize.SIWithDigits(2.2345e-12, 2, "F")) // Output: 2.23 pF fmt.Println(humanize.SIWithDigits(1000000, 0, "B")) // Output: 1 MB // ComputeSI for raw values value, prefix := humanize.ComputeSI(2.2345e-12) fmt.Printf("Value: %f, Prefix: %s\n", value, prefix) // Output: Value: 2.234500, Prefix: p } ``` -------------------------------- ### Format Word Series with Oxford Comma Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Creates a comma-separated list of words, including an Oxford comma before the final conjunction. ```go english.OxfordWordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar, and baz ``` -------------------------------- ### Format Integer to Ordinal String Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Converts an integer into its English ordinal representation (e.g., 1st, 2nd, 3rd, 4th). ```go fmt.Printf("You're my %s best friend.", humanize.Ordinal(193)) // You are my 193rd best friend. ``` -------------------------------- ### Format Floats with Thousand Separators (Commaf) Source: https://context7.com/dustin/go-humanize/llms.txt Use `humanize.Commaf` to format floating-point numbers with commas. `humanize.CommafWithDigits` can be used to limit the number of decimal places. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { fmt.Println(humanize.Commaf(834142.32)) fmt.Println(humanize.Commaf(1234567.891)) fmt.Println(humanize.Commaf(-98765432.1)) fmt.Println(humanize.CommafWithDigits(834142.3267, 1)) fmt.Println(humanize.CommafWithDigits(834142.3267, 2)) fmt.Println(humanize.CommafWithDigits(834142.3267, 0)) } ``` -------------------------------- ### Format big.Int with Thousand Separators (BigComma) Source: https://context7.com/dustin/go-humanize/llms.txt Use `humanize.BigComma` to format `math/big.Int` values with commas. This is suitable for very large numbers that exceed the `int64` range. ```go package main import ( "fmt" "math/big" "github.com/dustin/go-humanize" ) func main() { bigNum := big.NewInt(1234567890123456789) fmt.Println(humanize.BigComma(bigNum)) huge := new(big.Int) huge.SetString("123456789012345678901234567890", 10) fmt.Println(humanize.BigComma(huge)) negative := big.NewInt(-9876543210) fmt.Println(humanize.BigComma(negative)) } ``` -------------------------------- ### Format Integer with Commas Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Adds commas as thousands separators to an integer for better readability. ```go fmt.Printf("You owe $%s.\n", humanize.Comma(6582491)) // You owe $6,582,491. ``` -------------------------------- ### English Pluralization - Number and Word Form Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Generates a string with the count followed by the correct English word form for singular or plural nouns. Handles irregular plurals. ```go english.Plural(1, "object", "") // 1 object english.Plural(42, "object", "") // 42 objects english.Plural(2, "bus", "") // 2 buses english.Plural(99, "locus", "loci") // 99 loci ``` -------------------------------- ### Parse SI Notation Back to Numbers Source: https://context7.com/dustin/go-humanize/llms.txt Parses an SI-formatted string back into its numeric value and unit. Use this when you need to convert user input or data in SI notation back to a usable float64 and its corresponding unit. Error handling for invalid formats is recommended. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { // Parse SI notation value, unit, err := humanize.ParseSI("2.2345 pF") if err != nil { panic(err) } fmt.Printf("Value: %e, Unit: %s\n", value, unit) // Output: Value: 2.234500e-12, Unit: F value, unit, _ = humanize.ParseSI("1.5 kW") fmt.Printf("Value: %f, Unit: %s\n", value, unit) // Output: Value: 1500.000000, Unit: W value, unit, _ = humanize.ParseSI("100 mA") fmt.Printf("Value: %f, Unit: %s\n", value, unit) // Output: Value: 0.100000, Unit: A } ``` -------------------------------- ### Custom Number Formatting with FormatFloat Source: https://context7.com/dustin/go-humanize/llms.txt Formats numbers with custom thousand separators, decimal separators, and precision. This is useful for internationalization and presenting numbers in specific cultural formats. An empty format string uses a default US-like format. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { n := 12345.6789 // Standard US format fmt.Println(humanize.FormatFloat("#,###.##", n)) // Output: 12,345.67 // European format (space for thousands, comma for decimal) fmt.Println(humanize.FormatFloat("#\u202F###,##", n)) // Output: 12 345,68 // No decimals fmt.Println(humanize.FormatFloat("#,###.", n)) // Output: 12,345 // High precision fmt.Println(humanize.FormatFloat("#.###,######", n)) // Output: 12.345,678900 // Default format (empty string) fmt.Println(humanize.FormatFloat("", n)) // Output: 12,345.67 // Format integers fmt.Println(humanize.FormatInteger("#,###", 1234567)) // Output: 1,234,567 } ``` -------------------------------- ### Format Float to String without Trailing Zeros (Ftoa) Source: https://context7.com/dustin/go-humanize/llms.txt Use `humanize.Ftoa` to convert a float64 to a string, removing unnecessary trailing zeros. `humanize.FtoaWithDigits` allows limiting the number of decimal places. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { fmt.Printf("%f\n", 2.24) fmt.Println(humanize.Ftoa(2.24)) fmt.Printf("%f\n", 2.0) fmt.Println(humanize.Ftoa(2.0)) fmt.Println(humanize.Ftoa(3.14159)) fmt.Println(humanize.Ftoa(1000.50)) fmt.Println(humanize.FtoaWithDigits(3.14159, 2)) fmt.Println(humanize.FtoaWithDigits(3.14159, 4)) } ``` -------------------------------- ### Convert Numbers to Ordinal Strings (Ordinal) Source: https://context7.com/dustin/go-humanize/llms.txt Use `humanize.Ordinal` to convert an integer into its English ordinal string representation (e.g., 1st, 2nd, 3rd). Handles common English ordinal rules, including special cases for 11th, 12th, and 13th. ```go package main import ( "fmt" "github.com/dustin/go-humanize" ) func main() { fmt.Println(humanize.Ordinal(1)) fmt.Println(humanize.Ordinal(2)) fmt.Println(humanize.Ordinal(3)) fmt.Println(humanize.Ordinal(4)) fmt.Println(humanize.Ordinal(10)) fmt.Println(humanize.Ordinal(11)) fmt.Println(humanize.Ordinal(12)) fmt.Println(humanize.Ordinal(13)) fmt.Println(humanize.Ordinal(21)) fmt.Println(humanize.Ordinal(22)) fmt.Println(humanize.Ordinal(23)) fmt.Println(humanize.Ordinal(100)) fmt.Println(humanize.Ordinal(193)) } ``` -------------------------------- ### English Pluralization - Word Form Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Generates the correct English word form for singular or plural nouns based on a count. Handles irregular plurals. ```go english.PluralWord(1, "object", "") // object english.PluralWord(42, "object", "") // objects english.PluralWord(2, "bus", "") // buses english.PluralWord(99, "locus", "loci") // loci ``` -------------------------------- ### Format Float64 Removing Trailing Zeros Source: https://github.com/dustin/go-humanize/blob/master/README.markdown Formats a float64 value, removing unnecessary trailing zeros for a cleaner output. ```go fmt.Printf("%f", 2.24) // 2.240000 fmt.Printf("%s", humanize.Ftoa(2.24)) // 2.24 fmt.Printf("%f", 2.0) // 2.000000 fmt.Printf("%s", humanize.Ftoa(2.0)) // 2 ``` -------------------------------- ### Format Time to Relative String Source: https://context7.com/dustin/go-humanize/llms.txt The `Time` function formats a `time.Time` value into a human-readable relative time string, such as '3 hours ago' or '2 days from now'. It handles various durations, from seconds to years, and displays 'now' for very recent times. ```go package main import ( "fmt" "time" "github.com/dustin/go-humanize" ) func main() { // Time relative to now past := time.Now().Add(-3 * time.Hour) fmt.Println(humanize.Time(past)) // Output: 3 hours ago future := time.Now().Add(2 * 24 * time.Hour) fmt.Println(humanize.Time(future)) // Output: 2 days from now recent := time.Now().Add(-30 * time.Second) fmt.Println(humanize.Time(recent)) // Output: 30 seconds ago // Very recent times justNow := time.Now().Add(-500 * time.Millisecond) fmt.Println(humanize.Time(justNow)) // Output: now // Longer durations lastMonth := time.Now().Add(-45 * 24 * time.Hour) fmt.Println(humanize.Time(lastMonth)) // Output: 1 month ago lastYear := time.Now().Add(-400 * 24 * time.Hour) fmt.Println(humanize.Time(lastYear)) // Output: 1 year ago } ``` -------------------------------- ### Format Relative Time with Custom Labels Source: https://context7.com/dustin/go-humanize/llms.txt The `RelTime` function formats the relative time between two `time.Time` values using custom labels for past ('earlier', 'before') and future ('later', 'after') periods. This allows for flexible display of time differences. ```go package main import ( "fmt" "time" "github.com/dustin/go-humanize" ) func main() { start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) end := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC) // Custom labels fmt.Println(humanize.RelTime(start, end, "earlier", "later")) // Output: 2 weeks earlier fmt.Println(humanize.RelTime(end, start, "earlier", "later")) // Output: 2 weeks later // Using different label styles eventTime := time.Now().Add(-5 * time.Minute) fmt.Println(humanize.RelTime(eventTime, time.Now(), "before", "after")) // Output: 5 minutes before } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.