Sling Academy
Home/Golang/Changing Case: Uppercase, Lowercase, and Title Case in Go

Changing Case: Uppercase, Lowercase, and Title Case in Go

Last updated: November 24, 2024

When working with text in Go, it's often necessary to change its case. Whether you need to convert strings to uppercase, lowercase, or title case, Go provides functions that make these tasks straightforward.

Getting Started with the strings Package

Go provides the strings package, which includes functions to manipulate text. We'll explore how to use these functions for changing the case of strings.

Basic Example: Converting to Uppercase

The simplest way to convert a string to uppercase in Go is by using the strings.ToUpper function.

package main

import (
    "fmt"
    "strings"
)

func main() {
    original := "hello, world"
    upper := strings.ToUpper(original)
    fmt.Println(upper) // Output: HELLO, WORLD
}

Converting to Lowercase

To convert a string to lowercase, use the strings.ToLower function.

package main

import (
    "fmt"
    "strings"
)

func main() {
    original := "HELLO, WORLD"
    lower := strings.ToLower(original)
    fmt.Println(lower) // Output: hello, world
}

Intermediate Example: Converting to Title Case

For title casing, Go provides the strings.Title function (note that it's been deprecated in newer versions in favor of strings.ToTitle).

package main

import (
    "fmt"
    "strings"
)

func main() {
    original := "this is a title"
    title := strings.Title(original)
    fmt.Println(title) // Output: This Is A Title
}

With newer versions, it's recommended to use golang.org/x/text for language-specific transformations, but let’s stick with strings.ToTitle for this example:

package main

import (
    "fmt"
    "strings"
)

func main() {
    original := "this is a title"
    title := strings.ToTitle(original)
    fmt.Println(title) // Note: Output will still capitalize every letter, better options within external libraries
}

Advanced Example: Mixed Case Transformations

Let's say you want to capitalize every word but leave the rest in lowercase, akin to a proper title case. This requires a more advanced approach, possibly using external libraries or custom functions.

package main

import (
    "fmt"
    "strings"
    "unicode"
)

func toTitle(s string) string {
    // Split the string into words
    words := strings.Fields(s)
    // Capitalize the first letter of each word
    for i, word := range words {
        words[i] = strings.ToUpper(string(word[0])) + strings.ToLower(word[1:])
    }
    // Join the words back into a single string
    return strings.Join(words, " ")
}

func main() {
    original := "this is a proper title case"
    title := toTitle(original)
    fmt.Println(title) // Output: This Is A Proper Title Case
}

Here, we create a function that transforms each word independently: it capitalizes the first character and makes the rest of the word lowercase.

Conclusion

Case transformation in Go can be straightforward for simple use-cases using the built-in strings package. For more complex transformations, such as language-specific title casing, you might need to consider additional libraries that cater to text processing needs. Now you can manipulate string cases in your Go applications as needed.

Next Article: Validating and Parsing Email and URL Strings in Go

Previous Article: Detecting Palindromes Using Strings in Go

Series: Working with Strings in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant