In Go, strings.TrimSpace() is a built-in function that removes all leading and trailing white space (spaces, tabs, newlines, etc.) from a string.
Syntax
func TrimSpace(str string) string
Parameters
The str is the string to be trimmed.
Return value
The strings.TrimSpace() function returns the trimmed string.
Example
package main
import (
"fmt"
"strings"
)
func main() {
str := " This is a string with leading and trailing spaces. "
fmt.Println("Before:", str)
fmt.Println("After:", strings.TrimSpace(str))
}
Output
Before: This is a string with leading and trailing spaces.
After: This is a string with leading and trailing spaces.
In this example, strings.TrimSpace() removes the leading and trailing spaces from the string variable str. The output string is then printed to the console.
The strings.TrimSpace() function only removes leading and trailing spaces. If you want to remove spaces from within the string, you can use the strings.ReplaceAll() function to replace spaces with an empty string.
Conclusion
The strings.TrimSpace() is a built-in Golang function that removes all leading and trailing white spaces from a string.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.