August 31, 2025
Capitalize first character of each word in a string in Go
You can convert first character in upper case of each word in a string with using custom written function in Go lang. Function required a string type parameter and also return a string with first character in upper case of each word. Sample code is mentioned below:
Code
package main
import (
"fmt"
"strings"
)
func ucwords(str string) string {
words := strings.Fields(str)
for i, word := range words {
words[i] = strings.ToUpper(word[:1]) + word[1:]
}
return strings.Join(words, " ")
}
func main() {
greetingStr := "good morning"
fmt.Println(ucwords(greetingStr)) // print: Good Morning
}