Trimming the ends of a string
User input is usually messy, including additional spaces before or after the text that matters. This recipe shows how to use the string trimming functions for this purpose.
How to do it...
Use the strings.Trim
family of functions, as shown here:
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.TrimRight("Break-------", "-")) // Break fmt.Println(strings.TrimRight("Break with spaces-- -- --", "- ")) // Break with spaces fmt.Println(strings.TrimSuffix("file.txt", ".txt")) // file fmt.Println(strings.TrimLeft(" \t Indented text", " \t")) ...