To add an item to a Set in Go, you can use the built-in map data structure.
In this example,
mySet
with keys of type string
and values of type bool
.true
as the value for the key representing the item we want to add.package main
import "fmt"
func main() {
mySet := make(map[string]bool)
mySet["item1"] = true
fmt.Println(mySet)
}
map[item1:true]
In this example,
numberSet
with keys of type int
and values of type bool
.true
as the value for the key representing the integer we want to add.package main
import "fmt"
func main() {
numberSet := make(map[int]bool)
numberSet[123] = true
fmt.Println(numberSet)
}
map[123:true]
In this tutorial, we learned How to add an Item to a Set in Go language with well detailed examples.