Formatting with the correct plurals
When displaying messages for the user, the interaction is more pleasant if the sentences feel more human. The Go package golang.org/x/text
, which is the extension package, contains this feature for formatting plurals in the correct way.
Getting ready
Execute go get -x golang.org/x/text
to obtain the extension package in case you don't have it already.
How to do it...
- Open the console and create the folder
chapter03/recipe07
. - Navigate to the directory.
- Create the
plurals.go
file with the following content:
package main import ( "golang.org/x/text/feature/plural" "golang.org/x/text/language" "golang.org/x/text/message" ) func main() { message.Set(language.English, "%d items to do", plural.Selectf(1, "%d", "=0", "no items to do", plural.One, "one item to do", "<100", "%[1]d items to do", plural.Other, "lot of items to do", ...