OneBite.Dev - Coding blog in a bite size

capitalize a string in Go

Code snippet on how to capitalize a string in Go

// capitalize a string in Go

package main

import (
	"strings"
	"fmt"
)

func main() {
   s := "this is a string"
	fmt.Println(strings.Title(s))
}

This sample code will capitalize a string in Go. The package “strings” from the Go library is imported in order to use the “Title” function which will capitalize the string. A variable, “s,” is initialized to “this is a string”. The “Title” function is then used to capitalize the string and the result is printed using the “Println” function from the “fmt” package. The output should be “This Is A String”.

go