In Golang, you can use the strings.TrimSpace()
function to check if a string is empty or consists only of whitespace. Here is an example:
package main
import (
"fmt"
"strings"
)
func main() {
str := " " // Example string with whitespace
if strings.TrimSpace(str) == "" {
fmt.Println("String is empty or consists only of whitespace")
} else {
fmt.Println("String is not empty or does not consist only of whitespace")
}
}
In this example, the strings.TrimSpace()
function is used to remove any leading or trailing whitespace from the string str
. Then, the resulting string is checked with an empty string using the ==
operator. If it matches, the string is considered empty or consisting only of whitespace.