Happening Today!


In this article, I’ll show you how you can create better titles in Go. We’ll be using the strings library from the Go Standard Library for this tutorial.

You’ll often have a string input that you want to change the casing of, and it’s easy with Go.

Lower Case


If you want to change your text to lowercase, use the strings.ToLower method:

package main

import (
	"fmt"
	"strings"
)

func main() {
    fmt.Println(strings.ToLower("Change THIS strING to lowErCase"))
}

Your output should look like this:

String Handling Golang

It’s that easy. Uppercase and title case are similar calls:

Upper Case


strings.ToUpper("Your string")

Title Case


strings.ToTitle("Your String")

Here’s an example of all of them in action:

package main

import (
	"fmt"
	"strings"
)

func main() {

	sampleString := "this IS a Funny lOOKing StRING!"

	fmt.Printf("Lower case: %s\n", strings.ToLower(sampleString))
	fmt.Printf("Upper case: %s\n", strings.ToUpper(sampleString))
	fmt.Printf("Title case: %s\n", strings.Title(strings.ToLower(sampleString)))
}

Your output will look like this:

String Handling Golang

Notice that I pass in a lowercase version of the string to title:

fmt.Printf("Title case: %s\n", strings.Title(strings.ToLower(sampleString)))

I generally like to “normalize” the string before passing it to the Title method.

But here’s an even better way to do titles.

A Better Title Case


So, let’s say you have a title like this:

Welcome to the Dollhouse

Using Title(), it looks like this:

String Handling Golang

So “to” and “the” are capitalized, which isn’t exactly correct, and you wouldn’t likely want this in your titles.

I found this Recipe in The Go Cookbook that works great.

func properTitle(input string) string {
    words := strings.Fields(input)
    smallwords := " a an on the to "
    for index, word := range words {
        if strings.Contains(smallwords, " "+word+" ") {
            words[index] = word
        } else {
            words[index] = strings.Title(word)
        }
    }
    return strings.Join(words, " ")
}

So now, if we call the properTitle method, it looks like this:

String Handling Golang

It’s a great way to have better control over title casing.

An Even Better Title Case


But here’s a problem.

What if your title starts with a small word?

a night to remember

Then the output will look like this:

String Handling Golang

So let’s refactor it a bit.

Instead, we’ll use strings.Split to split up the string:

words := strings.Split(input, " ")

Then add the following condition to our if:

&& word != string(word[0])

So if the word is one of the small words and NOT at the beginning of the sentence, we won’t make it title case.

The function now looks like this:

func properTitle(input string) string {
    words := strings.Split(input, " ")
    smallwords := " a an on the to "

    for index, word := range words {
        if strings.Contains(smallwords, " "+word+" ") && word != string(word[0]) {
            words[index] = word
		} else {
			words[index] = strings.Title(word)
		}
	}
	return strings.Join(words, " ")
}

And the output looks like this:

String Handling Golang

Much better!

Summary

There are a few different ways to change casing in Go, and the strings library provides some great tools for doing so. There are ways to create titles, so they don’t look so goofy. I hope this helps you create better titles with Go.

Questions, comments? Let me know!


Learn how to leverage the Standard Library like a PRO! I just created a new course, The Go Standard Library, check it out!
Go Tutorials


Published: Aug 3, 2020 by Jeremy Morgan. Contact me before republishing this content.