The function strings.TrimSpace
is a built-in function in Golang that removes all leading and trailing whitespace characters from a string. It returns a new string that has no leading or trailing whitespace.
Here’s an example of how to use strings.TrimSpace
:
package main
import (
"fmt"
"strings"
)
func main() {
s := " hello world "
trimmed := strings.TrimSpace(s)
fmt.Println(trimmed) // output: "hello world"
}
In this example, we define a string s
with leading and trailing whitespace characters. We call strings.TrimSpace(s)
to create a new string without the leading and trailing whitespace. The output of this code will be "hello world"
.