Controlling case
There are a lot of practical tasks where the modification of case is the most common approach. Let's pick a few of these:
- Case-insensitive comparison
- Beginning the sentence with an automatic first capital
- Camel-case to snake-case conversion
For these purposes, the strings package offers functions ToLower, ToUpper, ToTitle, and Title.
How to do it...
- Open the console and create the folder
chapter02/recipe09. - Navigate to the directory.
- Create the
case.gofile with the following content:
package main
import (
"fmt"
"strings"
"unicode"
)
const email = "[email protected]"
const name = "isaac newton"
const upc = "upc"
const i = "i"
const snakeCase = "first_name"
func main() {
// For comparing the user input
// sometimes it is better to
// compare the input in a same
// case.
input := "[email protected]"
input = strings...