
How it works...
Trimming the string before it is handled by the code is pretty common practice, and as the preceding code demonstrates, it is easily done by the standard Go library. The strings library also provides more variations of the TrimXXX function, which also allows the trimming of other chars from the string.
To trim the leading and ending whitespace, the TrimSpace function of the strings package can be used. This typifies the following part of a code, which was also included in the example earlier:
stringToTrim := "\t\t\n Go \tis\t Awesome \t\t"
stringToTrim = strings.TrimSpace(stringToTrim)
The regex package is suitable for replacing multiple spaces and tabs, and the string can be prepared for further processing this way. Note that, with this method, the break lines are replaced with a single space.
This part of the code represents the use of the regular expression to replace all multiple whitespaces with a single space:
r := regexp.MustCompile("\\s+")
replace := r.ReplaceAllString(stringToTrim, " ")
Padding is not an explicit function for the strings package, but it can be achieved by the Sprintf function of the fmt package. The pad function in code uses the formatting pattern % <+/-padding>s and some simple math to find out the padding. Finally, the minus sign before the padding number works as the right pad, and the positive number as the left pad.