To convert a string to uppercase or lowercase using strconv in Golang, you can use the functions provided by the strconv package, specifically the functions ToLower and ToUpper.
Here is an example code that demonstrates the conversion:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World!"
// Converting string to lowercase
lowerStr := strings.ToLower(str)
fmt.Println(lowerStr) // Output: hello, world!
// Converting string to uppercase
upperStr := strings.ToUpper(str)
fmt.Println(upperStr) // Output: HELLO, WORLD!
}
In this example, we import the "strings" package along with the "fmt" package. Then, we define a string variable str
and initialize it with "Hello, World!".
To convert the string to lowercase, we use strings.ToLower(str)
, which returns a new string with all characters in lowercase. The result is stored in lowerStr
, and it is then printed to the console.
Similarly, to convert the string to uppercase, we use strings.ToUpper(str)
, which returns a new string with all characters in uppercase. The result is stored in upperStr
, and it is printed to the console as well.
You can replace strings.ToLower
with strconv.ToLower
and strings.ToUpper
with strconv.ToUpper
if you specifically want to use the strconv
package for these conversions. However, in this case, using the strings
package is more straightforward.