Learn to Capitalize the First Letter of Each Word with Go

Hey, there! Golang enthusiastics. I hope you are putting effort in the Golang journey. This week we are improving the learning by using Go’s internal packages. We are using string package.

Problem Statement

Write a function that takes a st…


This content originally appeared on DEV Community and was authored by Ruben Alvarado

Hey, there! Golang enthusiastics. I hope you are putting effort in the Golang journey. This week we are improving the learning by using Go’s internal packages. We are using string package.

Problem Statement

Write a function that takes a string as input and returns the string with the first letter of each word capitalized. The words are separated by spaces.

Breaking down the problem statement: we have an input—a string of words separated by spaces. We need to process each word to capitalize its first letter. Finally, our output will be the same string, but processed.

Now it's time to define our algorithm to process the string and obtain the requested output.

Algorithm to Solve it

First, we need to split the whole string into words. This is where the string package comes to save the day—without it, we'd have to find each space in the string and split from there. Better to choose the correct tool.

words := strings.Fields(s)

Using the strings package and its Fields method, we split our string into a slice of words separated by spaces. Easy, right? Don't forget to import the strings package.

Now, using a for range loop, we'll iterate through our slice of words, obtaining both the index and the word itself.

for i, word := range words {}

For each string, we'll pick the first letter and replace it with the same character uppercased. To do this, we're going to use an external package called cases in combination with the language package.

This line tells Go that the word variable is a string that should be titled (capitalize its first letter) in English. The result is then reassigned to the word at the current index in the words slice.

words[i] = cases.Title(language.English).String(word)

Finally, once the entire slice has been processed, return it as a single string. Use the strings package again to join the slice elements with blank spaces between them.

return strings.Join(words, " ")

And there you have it! You've successfully capitalized the first letter of each word in the string using the right tools.

Final Thoughts

As you can see, leveraging Go's built-in and external packages leads to cleaner, more maintainable code. Rather than implementing complex string manipulation from scratch, we can rely on well-tested libraries that solve common problems efficiently.

With just a handful of lines, Go enables us to write elegant solutions that are both readable and performant. This approach not only saves development time but also reduces the potential for bugs.

Don't forget to declare your modules to use the cases and language packages—they aren't built-in ones. As always, you can find the complete code in my GitHub repo: https://github.com/RubenOAlvarado/algorithms

Keep learning, and I'll see you in the next one!

Photo by Algebra Digital on Unsplash


This content originally appeared on DEV Community and was authored by Ruben Alvarado


Print Share Comment Cite Upload Translate Updates
APA

Ruben Alvarado | Sciencx (2025-11-04T00:12:05+00:00) Learn to Capitalize the First Letter of Each Word with Go. Retrieved from https://www.scien.cx/2025/11/04/learn-to-capitalize-the-first-letter-of-each-word-with-go/

MLA
" » Learn to Capitalize the First Letter of Each Word with Go." Ruben Alvarado | Sciencx - Tuesday November 4, 2025, https://www.scien.cx/2025/11/04/learn-to-capitalize-the-first-letter-of-each-word-with-go/
HARVARD
Ruben Alvarado | Sciencx Tuesday November 4, 2025 » Learn to Capitalize the First Letter of Each Word with Go., viewed ,<https://www.scien.cx/2025/11/04/learn-to-capitalize-the-first-letter-of-each-word-with-go/>
VANCOUVER
Ruben Alvarado | Sciencx - » Learn to Capitalize the First Letter of Each Word with Go. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/04/learn-to-capitalize-the-first-letter-of-each-word-with-go/
CHICAGO
" » Learn to Capitalize the First Letter of Each Word with Go." Ruben Alvarado | Sciencx - Accessed . https://www.scien.cx/2025/11/04/learn-to-capitalize-the-first-letter-of-each-word-with-go/
IEEE
" » Learn to Capitalize the First Letter of Each Word with Go." Ruben Alvarado | Sciencx [Online]. Available: https://www.scien.cx/2025/11/04/learn-to-capitalize-the-first-letter-of-each-word-with-go/. [Accessed: ]
rf:citation
» Learn to Capitalize the First Letter of Each Word with Go | Ruben Alvarado | Sciencx | https://www.scien.cx/2025/11/04/learn-to-capitalize-the-first-letter-of-each-word-with-go/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.