Welcome To Golang By Example

Capitalize a string in Go (Golang)

Table of Contents

Overview

In Golang string are UTF-8 encoded. strings package of go provides a Title method that can be used to convert all first letters of all words in a sentence as capital. It returns a copy of the string with all first letters of the words capitalized.

Below is the signature of the function

func Title(s string) string

Let’s look at the working code

Code:

package main

import (
    "fmt"
    "strings"
)

func main() {
    res := strings.Title("this is a test sentence")
    fmt.Println(res)
}

Output:

This Is A Test Sentence