How to check if a string is empty in Go
To check if a given string is empty or not, we can use the built-in len()
function in Go language.
Here is an example:
package main
import "fmt"
func main() {
str := "";
if(len(str) == 0){
fmt.Println("String is empty")
}else{
fmt.Println("String is not empty")
}
}
Output:
String is empty
or we can use the str == ""
to check if a string is empty.
package main
import "fmt"
func main() {
str := "Hello";
if(str == ""){
fmt.Println("String is empty")
}else{
fmt.Println("String is not empty")
}
}
Output:
String is not empty