To get the size of a set in Go, you can use the built-in len
function on the set.
In this example,
set1
using a map with boolean values to represent a set-like structure.len
function on the set set1
to get the number of elements in the set.len
function returns the size of the set.package main
import "fmt"
func main() {
set1 := make(map[string]bool)
set1["apple"] = true
set1["banana"] = true
set1["cherry"] = true
size := len(set1)
fmt.Println("Size of set1:", size)
}
Size of set1: 3
In this example,
emptySet
using an empty map.len
function on the set emptySet
to get the number of elements in the set.len
function returns 0.package main
import "fmt"
func main() {
emptySet := make(map[string]bool)
size := len(emptySet)
fmt.Println("Size of emptySet:", size)
}
Size of emptySet: 0
In this tutorial, we learned How to Get Length of a Set in Go language with well detailed examples.