March 2020
Intermediate to advanced
406 pages
8h 39m
English
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 mainimport "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.
Read now
Unlock full access