How to capitalize the first letter of a string

Issue

I have a string like this

var sentence string = "the biggest ocean is the Pacific ocean"

I want to be able to capitalize the first letter t in the input string, so that the string becomes

"The biggest ocean is the Pacific ocean"

How to do that in Go?

I have tried using strings.Title and strings.ToTitle however they don’t do what I want.

Solution

I have simple solution for you.

Its a fork I have of someones project on Github

https://github.com/CleanMachine1/capitalise

To use it just run in a terminal:

go mod init MODULENAME
go get github.com/cleanmachine1/capitalise

then in your code you can use


package main

import ("github.com/cleanmachine1/capitalise")

func main(){
 sentence = capitalise.First(sentence)
}

Answered By – CleanMachine1

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.