set

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 2 Imported by: 0

README

Go Set

Set type for the Go language.

Installation

go get github.com/pvargas/go-set

Usage

This set implementation supports Go hashable comparable types. Non-comparable types are not supported and cannot be used as set elements.

Creating a New Set

There are two ways to create a set. The first way is by using the NewSet method.

NewSet is variadic, so any number of arguments can be provided. If no arguments are provided, the desired set type must be specified. The resulting set will not contain any repeated argument values.

package example

import "github.com/pvargas/go-set"

// Empty set of runes
words := set.NewSet[rune]()

// Heterogenous set
numbers := set.NewSet[any](2, "three", 5.0, '7', 11i)

names := []string{"Alice", "Bob", "Bob", "Carol"}

// Set of three elements (Alice, Bob, and Carol) 
// created from a slice of four elements. 
nameSet := set.NewSet(names...)

The second way to create a set is by using Go's make function.

someSet := make(set.Set[string])
Basic Operations
Method Description
Contains Returns true if the given value is found in the set
Elements Returns a slice containing all members of the set
Insert Takes a value and adds it to the set
NewSet Takes a variable number of arguments and returns a set containing those values
Remove Deletes a given value from the set
Mathematical Set Operations

TBD

Documentation

Overview

Package set defines a set type, a mutable, unordered collection of unique elements, analogous to Python's set collection, as well as functions to perform typical mathematical set operations.

The underlying type of this set implementation is a map. This means that all operations that can be done with maps can also be done with sets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] map[T]struct{}

Set is a map where the key type is generic and all values are empty structs that take up no space in memory.

func NewSet

func NewSet[T comparable](items ...T) Set[T]

NewSet takes a variable number of arguments and returns a set containing those values.

func (*Set[T]) Contains

func (set *Set[T]) Contains(item T) bool

Contains returns true if the given value is found in the set. Otherwise, returns false.

func (*Set[T]) Elements

func (set *Set[T]) Elements() []T

Elements returns a slice containing all members of the set.

func (*Set[T]) Insert

func (set *Set[T]) Insert(item T)

Insert takes a value and adds it to the set.

func (*Set[T]) Remove

func (set *Set[T]) Remove(item T)

Remove deletes a given value from the set, if present.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL