Check if a string is empty in Go
There are several ways to check if a string is empty in Go, and in this article, we will explore the most common methods. We will cover using the len()
function, the ==
operator, the strings.TrimSpace()
function, and the strings.Trim()
function.
Using the len()
function:
In the below example, we check the length of the string, and if it is equal to zero, the string is empty. However, if the string has white space characters, the length will be greater than 0, it returns as a non-empty string.
package main
import (
"fmt"
)
func main() {
checkStringEmpty("") // Empty string
checkStringEmpty(" ") // string with whitespace
checkStringEmpty("Golang") // Non Empty string
}
func checkStringEmpty(str string) {
// Using the len() function
if len(str) == 0 {
fmt.Println("The string is empty.")
} else {
fmt.Println("The string is not empty.")
}
}
Output
The string is empty.
The string is not empty.
The string is not empty.
Using the ==
operator:
In the below example, we compare the string to an empty string; if they are equal, the string is empty; else, it's not empty.
package main
import (
"fmt"
)
func main() {
checkStringEmpty("") // Empty string
checkStringEmpty(" ") // string with whitespace
checkStringEmpty("Golang") // Non Empty string
}
func checkStringEmpty(str string) {
// Using the == operator
if str == "" {
fmt.Println("The string is empty.")
} else {
fmt.Println("The string is not empty.")
}
}
Output
The string is empty.
The string is not empty.
The string is not empty.
Using the strings.TrimSpace()
function:
This removes any leading and trailing whitespace from the string and then compares it with an empty string. If they are equal, the string is empty; else, it's not empty.
package main
import (
"fmt"
"strings"
)
func main() {
checkStringEmpty("") // Empty string
checkStringEmpty(" ") // string with whitespace
checkStringEmpty("Golang") // Non Empty string
}
func checkStringEmpty(str string) {
// Using the strings.TrimSpace() function
if strings.TrimSpace(str) == "" {
fmt.Println("The string is empty.")
} else {
fmt.Println("The string is not empty.")
}
}
Output
The string is empty.
The string is empty.
The string is not empty.
Using the strings.Trim()
function:
The strings.Trim()
function removes the specified characters from the string and compares them with an empty string. If they are equal, the string is empty; else, it's not empty.
package main
import (
"fmt"
"strings"
)
func main() {
checkStringEmpty("") // Empty string
checkStringEmpty(" ") // string with whitespace
checkStringEmpty("Golang") // Non Empty string
}
func checkStringEmpty(str string) {
// Using the strings.Trim() function
if strings.Trim(str, " ") == "" {
fmt.Println("The string is empty.")
} else {
fmt.Println("The string is not empty.")
}
}
Output
The string is empty.
The string is empty.
The string is not empty.
Conclusion
In this article, we have looked at several ways to check if a string is empty in Golang, including using the len()
function, the ==
operator, the strings.TrimSpace()
function, and the strings.Trim()
function.
It is important to note that checking for an empty string is not only limited to these methods; there are other ways to check for an empty string. However, these are some of the most commonly used methods and should cover most use cases.