To check if a Set is empty in Go, you can compare the length of the Set to 0.
In this example,
set
and initialize it with some elements, or an empty Set if there are no elements.set
is equal to 0.package main
import (
"fmt"
)
func main() {
set := make(map[string]bool) // Or initialize with elements for a non-empty Set
isEmpty := len(set) == 0
if isEmpty {
fmt.Println("The Set is empty")
} else {
fmt.Println("The Set is not empty")
}
}
The Set is empty
In this example,
emptySet
.emptySet
is equal to 0.package main
import (
"fmt"
)
func main() {
emptySet := make(map[string]bool)
isEmpty := len(emptySet) == 0
if isEmpty {
fmt.Println("The Set is empty")
} else {
fmt.Println("The Set is not empty")
}
}
The Set is empty
In this tutorial, we learned How to check if a Set is Empty in Go language with well detailed examples.