To check if a specific item is present in a Set in Go, you can use a map with a boolean value indicating presence.
In this example,
mySet
with keys as the elements of the set and boolean values indicating presence.true
for items present in the set.true
if the item is present in the set, otherwise it will be false
.package main
import "fmt"
func main() {
// Define map representing the Set
mySet := map[string]bool{"apple": true, "banana": true, "orange": true, "grape": true, "melon": true}
// Check if item is present
item := "banana"
isPresent := mySet[item]
// Print result
fmt.Printf("Is %s present in the Set? %t\n", item, isPresent)
}
Is banana present in the Set? true
In this example,
mySet
with keys as the elements of the set and boolean values indicating presence.true
for items present in the set.false
if the item is not present in the set.package main
import "fmt"
func main() {
// Define map representing the Set
mySet := map[string]bool{"apple": true, "banana": true, "orange": true, "grape": true, "melon": true}
// Check if item is not present
item := "peach"
isNotPresent := !mySet[item]
// Print result
fmt.Printf("Is %s not present in the Set? %t\n", item, isNotPresent)
}
Is peach not present in the Set? true
In this tutorial, we learned How to check if a Specific Item is present in the Set in Go language with well detailed examples.