To create an empty set in Go, you can use a map with keys of the desired type and boolean values.
In this example,
true
.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 an empty map using the make
function. The keys of this map are of type string
and the values are of type bool
.set2
by setting the keys to the desired values and the values to true
. For example, we add the strings "apple"
and "banana"
.set2
to the console to verify its contents. The map will show the keys we added with values of true
.package main
import "fmt"
func main() {
set2 := make(map[string]bool)
set2["apple"] = true
set2["banana"] = true
fmt.Println("Contents of set2:", set2)
}
Contents of set2: map[apple:true banana:true]
In this tutorial, we learned How to create an Empty Set in Go language with well detailed examples.