To check if a string is empty in Go, you can compare the length of the string with 0.
In this example,
str1
with an empty string value ""
.str1
with 0
to check if it is empty.0
, the string is empty.package main
import (
"fmt"
)
func main() {
str1 := ""
isEmpty := len(str1) == 0
fmt.Println("Is string empty:", isEmpty)
}
Is string empty: true
In this example,
str2
with a non-empty string value "Hello World"
.str2
with 0
to check if it is empty.0
, the string is empty.package main
import (
"fmt"
)
func main() {
str2 := "Hello World"
isEmpty := len(str2) == 0
fmt.Println("Is string empty:", isEmpty)
}
Is string empty: false
In this tutorial, we learned How to Check if String is Empty in Go language with well detailed examples.