Provides English-specific utilities for pluralization and formatting word lists with conjunctions and Oxford commas.
import "github.com/dustin/go-humanize/english"func Plural(quantity int, singular, plural string) stringFormats an integer and a string into a single pluralized string. Returns the quantity followed by the appropriate singular or plural form of the word.
Parameters:
quantity - The number of itemssingular - The singular form of the wordplural - The plural form of the word (use empty string for regular pluralization)Returns: String with quantity and pluralized word (e.g., "1 object", "42 objects")
Example:
english.Plural(1, "object", "") // "1 object"
english.Plural(42, "object", "") // "42 objects"
english.Plural(0, "object", "") // "0 objects"
english.Plural(2, "bus", "") // "2 buses"
english.Plural(1, "child", "children") // "1 child"
english.Plural(5, "child", "children") // "5 children"
english.Plural(99, "locus", "loci") // "99 loci"func PluralWord(quantity int, singular, plural string) stringReturns just the pluralized word form (without the quantity) based on the quantity. Uses simple English pluralization rules if the plural form is not explicitly provided.
Parameters:
quantity - The number of items (determines singular vs plural)singular - The singular form of the wordplural - The plural form of the word (use empty string for regular pluralization)Returns: The appropriate word form (e.g., "object" or "objects")
Example:
english.PluralWord(1, "object", "") // "object"
english.PluralWord(42, "object", "") // "objects"
english.PluralWord(0, "object", "") // "objects"
english.PluralWord(2, "bus", "") // "buses"
english.PluralWord(1, "child", "children") // "child"
english.PluralWord(5, "child", "children") // "children"
english.PluralWord(99, "locus", "loci") // "loci"When the plural form is an empty string, the function applies regular English pluralization rules:
Note: These rules handle many common cases but not all irregular plurals. For irregular plurals (e.g., "child" → "children", "person" → "people"), provide the explicit plural form.
func WordSeries(words []string, conjunction string) stringConverts a list of words into a word series in English without using an Oxford comma (serial comma).
Parameters:
words - Slice of words to formatconjunction - The conjunction to use (typically "and" or "or")Returns: Formatted word series
Example:
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" (no Oxford comma)
english.WordSeries([]string{"red", "green", "blue", "yellow"}, "or")
// "red, green, blue or yellow"
english.WordSeries([]string{}, "and")
// ""
english.WordSeries([]string{"Alice", "Bob", "Charlie"}, "and")
// "Alice, Bob and Charlie"func OxfordWordSeries(words []string, conjunction string) stringConverts a list of words into a word series in English using an Oxford comma (serial comma) before the final conjunction.
Parameters:
words - Slice of words to formatconjunction - The conjunction to use (typically "and" or "or")Returns: Formatted word series with Oxford comma
Example:
english.OxfordWordSeries([]string{"foo"}, "and")
// "foo"
english.OxfordWordSeries([]string{"foo", "bar"}, "and")
// "foo and bar"
english.OxfordWordSeries([]string{"foo", "bar", "baz"}, "and")
// "foo, bar, and baz" (Oxford comma before "and")
english.OxfordWordSeries([]string{"red", "green", "blue", "yellow"}, "or")
// "red, green, blue, or yellow"
english.OxfordWordSeries([]string{}, "and")
// ""
english.OxfordWordSeries([]string{"Alice", "Bob", "Charlie"}, "and")
// "Alice, Bob, and Charlie"count := 5
message := fmt.Sprintf("You have %s",
english.Plural(count, "unread message", ""))
// Output: "You have 5 unread messages"users := []string{"Alice", "Bob", "Charlie"}
fmt.Printf("Online users: %s",
english.WordSeries(users, "and"))
// Output: "Online users: Alice, Bob and Charlie"
// Or with Oxford comma:
fmt.Printf("Online users: %s",
english.OxfordWordSeries(users, "and"))
// Output: "Online users: Alice, Bob, and Charlie"matches := 3
fmt.Printf("Found %s",
english.Plural(matches, "match", "matches"))
// Output: "Found 3 matches"
matches = 1
fmt.Printf("Found %s",
english.Plural(matches, "match", "matches"))
// Output: "Found 1 match"options := []string{"save", "cancel", "help"}
fmt.Printf("Available commands: %s",
english.OxfordWordSeries(options, "or"))
// Output: "Available commands: save, cancel, or help"childCount := 3
fmt.Printf("The family has %s",
english.Plural(childCount, "child", "children"))
// Output: "The family has 3 children"
mouseCount := 2
fmt.Printf("Detected %s",
english.Plural(mouseCount, "mouse", "mice"))
// Output: "Detected 2 mice"// Get just the word form for complex messages
numFiles := 42
fileWord := english.PluralWord(numFiles, "file", "")
numFolders := 1
folderWord := english.PluralWord(numFolders, "folder", "")
fmt.Printf("Selected %d %s and %d %s",
numFiles, fileWord, numFolders, folderWord)
// Output: "Selected 42 files and 1 folder"// Both WordSeries functions handle empty slices gracefully
items := []string{}
result := english.WordSeries(items, "and")
// result = ""
if result == "" {
fmt.Println("No items available")
}WordSeries without it or OxfordWordSeries with it.