Set

A set is used to only store keys. Go doesn't have a set type, so a map of the type to a Boolean value is used frequently in order to build a set. The following code block is an implementation of an STL equivalent set:

package main

import "fmt"

func main() {
s := make(map[int]bool)

for i := 0; i < 5; i++ {
s[i] = true
}

delete(s, 4)

if s[2] {
fmt.Println("s[2] is set")
}
if !s[4] {
fmt.Println("s[4] was deleted")
}
}

The resulting output shows that we were able to set and delete the values accordingly:

We can see from our output that our code can properly manipulate a set, which is crucial for common key–value pairings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.76.72