To convert the first character in a string to uppercase in Go, you can use the strings.ToUpper()
function along with string slicing.
In this example,
strings
package.str1
with a value of "hello world"
.str1
to uppercase using strings.ToUpper()
function.package main
import (
"fmt"
"strings"
)
func main() {
str1 := "hello world"
modifiedStr1 := strings.ToUpper(string(str1[0])) + str1[1:]
fmt.Println("Modified string is:", modifiedStr1)
}
Modified string is: Hello world
In this example,
strings
package.str2
with a value of "golang"
.str2
to uppercase using strings.ToUpper()
function.package main
import (
"fmt"
"strings"
)
func main() {
str2 := "golang"
modifiedStr2 := strings.ToUpper(string(str2[0])) + str2[1:]
fmt.Println("Modified string is:", modifiedStr2)
}
Modified string is: Golang
In this tutorial, we learned How to convert First Character in String to Uppercase in Go language with well detailed examples.