To convert a string to uppercase in Go, use the strings.ToUpper()
function. It returns a copy of the input string, in which all letters are uppercase. The function is part of the built-in strings
package used for manipulating UTF-8 encoded strings.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ToUpper("https://gosamples.dev"))
}
Output:
HTTPS://GOSAMPLES.DEV
If you want to uppercase only the first letter of each word, see our other example here.
If your string is in a language with the special casing rules, for example, Turkish or Azeri, use the strings.ToUpperSpecial()
function. This function takes a case mapping as its first parameter, with which it converts the string into uppercase.
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "en iyi web sitesi"))
}
Output
EN Δ°YΔ° WEB SΔ°TESΔ°