Capitalize first character of a string in Go

Go doesn’t have a built-in function to convert first character in small case. But you can convert first character in upper case of a string into upper case with using code mentioned in example. Example of capitalize method is mentioned below: Code


package main

import (
	"fmt"
	"strings"
)

func ucfirst(s string) string {
	if len(s) == 0 {
		return ""
	}
	return strings.ToUpper(s[:1]) + s[1:]
}

func main() {
	str := "hello world"
	fmt.Println(ucfirst(str))  // Output: Hello world
}