To access items in a set in Go, you can use a map data structure.
In this example,
set
with initial values representing a set.package main
import (
"fmt"
)
func main() {
// Create a set using a map with initial values
set := map[string]bool{
"apple": true,
"banana": true,
"cherry": true,
}
// Access items in the set
fmt.Println("Items in the set:")
for item := range set {
fmt.Println(item)
}
// Check if an item exists in the set
target := "banana"
if set[target] {
fmt.Println(target, "is in the set")
} else {
fmt.Println(target, "is not in the set")
}
}
Items in the set: apple banana cherry banana is in the set
In this tutorial, we learned How to Access Items in a Set in Go language with well detailed examples.