golang

Golang Set

If you are coming from a programming language, such as Python, you are familiar with the concept of sets.

A set refers to a collection of unordered items. Sets are unique and do not allow duplicate values in a single entry.

If you work with Go programming, you quickly realize there are no concept sets. However, we can play some tricks with maps and structs to create a set.

Golang Create Set

Fundamentally, sets are a collection of unique values. We can use a map with an empty struct to represent them.

We can express this syntax as:

var myset map[type]struct{}

Since an empty struct takes 0 bytes, the set is a very efficient method of implementing a set.

The following example creates a simple set using the previous syntax.

package main
type void struct{}
var member void
func main() {
    set := make(map[string]void)
}

The previous example creates an empty set with a string as the key and an empty struct.

Golang Add Elements to Set

Once we have the set declared, we can add elements to the map using the map syntax. An example is as illustrated below:

package main
import "fmt"

type void struct{}
var member void
func main() {
    set := make(map[string]void)
    set["apple"] = member
    set["orange"] = member
    set["mango"] = member
    fmt.Println(set)
}

In the previous example, we assign new values to the set.

Golang Set Iteration

We can iterate over a map using the classic for loop and range operator. A code example is shown below:

package main
import "fmt"
type void struct{}
var member void
func main() {
    set := make(map[string]void)
    set["apple"] = member
    set["orange"] = member
    set["mango"] = member
    for k := range set {
        fmt.Println(k)
    }
}

The loop construct above should return the elements in the set:

$ go run sets.go
mango
apple
orange

Golang Delete Set Element

We can remove members from a set using the Delete method. For example, to remove the element “apple” from the set, we can do:

package main
import “fmt”
type void struct{}
var member void
func main() {
    set := make(map[string]void)
    set[“apple”] = member
    set[“orange”] = member
    set[“mango”] = member
    delete(set, “apple”)
    for k := range set {
        fmt.Println(k)
    }
}

The Delete method takes the name of the set and the element to remove.

Golang Check If Element Exists

We can check if an element exists within a set using the syntax shown:

if _, ok := set[“apple”]; ok {
    fmt.Println(“element exists”)
} else {
    fmt.Println(“element not found”)
}

The previous code should execute the if block when the element is in the set and the else block if otherwise.

Conclusion

This article explored how to implement a set type in the Go programming language. Before using the set for performance critical environments, check how a set affects the performance. We hope you found this article helpful. Check other Linux Hint articles for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list