go-set

Run CI Tests

Provides the set package that implements a generic mathematical set for Go. The package only provides a basic implementation that is optimized for correctness and convenience. This package is not thread-safe.

Documentation

The full documentation is available on pkg.dev.

Motivation

Package set helps reduce the boiler plate of using a map[<type>]struct{} as a set.

Say we want to de-duplicate a slice of strings

items := []string{"mitchell", "armon", "jack", "dave", "armon", "dave"}

A typical example of the classic way using map built-in:

m := make(map[string]struct{})
for _, item := range items {
  m[item] = struct{}{}
}
list := make([]string, 0, len(items))
for k := range m {
  list = append(list, k)
}

The same result, but in one line using package go-set.

list := set.From[string](items).List()

Methods

Implements the following set operations

  • Insert
  • InsertAll
  • InsertSet
  • Remove
  • RemoveAll
  • RemoveSet
  • Contains
  • ContainsAll
  • Subset
  • Size
  • Union
  • Difference
  • Intersect

Provides helper methods

  • Equal
  • Copy
  • List
  • String

Install

go get github.com/hashicorp/[email protected]
import "github.com/hashicorp/go-set"

Example

Below are simple example of usages

s := set.New[int](10)
s.Insert(1)
s.InsertAll([]int{2, 3, 4})
s.Size() # 3

s := set.From[string]([]string{"one", "two", "three"})
s.Contains("three") # true
s.Remove("one") # true

a := set.From[int]([]int{2, 4, 6, 8})
b := set.From[int]([]int{4, 5, 6})
a.Intersect(b) # {4, 6}

GitHub

View Github