To initialize a set in Go, you can use a map with keys of the desired type and boolean values.
In this example,
set1
and assign it an empty map using the make
function. The keys of this map are of type string
and the values are of type bool
.set1
to the console to verify its contents. An empty map in Go is represented as map[]
.package main
import "fmt"
func main() {
set1 := make(map[string]bool)
fmt.Println("Contents of set1:", set1)
}
Contents of set1: map[]
In this example,
set2
and assign it a map with elements using the make
function. The keys of this map are of type string
and the values are set to true
.set2
to the console to verify its contents.package main
import "fmt"
func main() {
set2 := make(map[string]bool)
set2["apple"] = true
set2["banana"] = true
set2["cherry"] = true
fmt.Println("Contents of set2:", set2)
}
Contents of set2: map[apple:true banana:true cherry:true]
In this tutorial, we learned How to Initialize a Set in Go language with well detailed examples.