How to Make First Letter of Words Uppercase in a String in Go

Created
Modified

Using cases Package

install

Use the following command to download the repository to the local file system.

install
go mod tidy
go get golang.org/x/text/cases
go get golang.org/x/text/language
go install golang.org/x/text/cases@latest
go install golang.org/x/text/language@latest

Note that since Go 1.17 installing packages with go get is deprecated:

cases Usage

Package cases provides general and language-specific case mappers.

See the following example:

package main

import (
  "fmt"

  "golang.org/x/text/cases"
  "golang.org/x/text/language"
)

func main() {
  s := "hello world!"

  for _, c := range []cases.Caser{
    cases.Title(language.Dutch),
    cases.Title(language.Und, cases.NoLower),
  } {

    fmt.Println(c.String(s))
  }
}
Hello World!
Hello World!

Related Tags