set

package
v1.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

set

Generic helper functions for sets represented as plain map[T]struct{} values. The package defines no set type: it works on map values directly, so zero values, range iteration and len stay the native map ones.

import "github.com/domonda/go-types/set"

Prefer mapset for new code. It covers the full set algebra, works with map[K]bool sets and any comparable element type, and mirrors the container/mapset package proposed for Go 1.28, so it can be swapped for the standard library one by changing the import path. This package predates it and is kept unchanged for its existing callers. Two helpers here have no direct mapset counterpart: Add(set, values...) allocates a nil set and returns it, where mapset.Insert panics on a nil set and takes a single element, and ContainsAny and ContainsAll take variadic values, where mapset offers Intersects and ContainsAll(x, iter.Seq[K]).

For a set type with methods, see the root package type types.Set[T]. It is a defined map type map[T]struct{} with the set algebra of mapset as methods and JSON marshalling as a sorted array.

Functions

All functions take and return the unnamed type map[T]struct{} with T cmp.Ordered. Values of a defined map type with that underlying type, like types.Set[T] or strutil.StringSet, can be passed directly.

Function Description
New(values...) New set with values. Never nil, New[int]() is an empty set.
Add(set, values...) Adds values and returns the set. A nil set is allocated first, so always use the result.
Contains(set, v) Whether v is in the set.
ContainsAll(set, values...) Whether every value is in the set. True for no values.
ContainsAny(set, values...) Whether at least one value is in the set. False for no values.
ContainsAllOther(set, other) Whether every element of other is in the set. True for an empty other.
ContainsAnyOther(set, other) Whether at least one element of other is in the set. False for an empty other.

Every function that takes a set accepts a nil set as the empty set. Only Add writes to it.

Migrating to mapset:

set mapset
New(values...) Of(values...)
Add(set, values...) InsertAll(set, slices.Values(values)) on a non-nil set
Contains(set, v) Contains(set, v)
ContainsAll(set, values...) ContainsAll(set, slices.Values(values))
ContainsAny(set, values...) Intersects(set, Of(values...))
ContainsAllOther(set, other) ContainsAll(set, All(other))
ContainsAnyOther(set, other) Intersects(set, other)

Serialization

The package has no serialization code. A map[T]struct{} encodes with encoding/json as an object with empty object values, like {"a":{},"b":{}}, a nil map as null. Use types.Set[T] for JSON arrays. There is no SQL support.

Gotchas

  • T must be cmp.Ordered although the functions only need comparable, so uu.IDSet ([16]byte keys) and struct keys don't work. Use mapset.
  • Add returns the set. Calling set.Add(s, v) on a nil s without assigning the result loses the new map.
  • The functions take map[T]struct{}, not map[T]bool. Use mapset for map[T]bool sets.
  • Like any Go map, a set is not safe for concurrent writes.

Examples

package main

import (
	"encoding/json"
	"fmt"

	"github.com/domonda/go-types/set"
)

func main() {
	tags := set.New("go", "types", "validation")
	tags = set.Add(tags, "json")

	fmt.Println(set.Contains(tags, "go"))
	fmt.Println(set.ContainsAll(tags, "go", "json"))
	fmt.Println(set.ContainsAny(tags, "rust"))
	fmt.Println(set.ContainsAll(tags), set.ContainsAny(tags))

	// Add allocates the map for a nil set
	var ids map[int]struct{}
	ids = set.Add(ids, 3, 1, 3)
	fmt.Println(len(ids), set.ContainsAllOther(ids, set.New(1)), set.ContainsAnyOther(ids, set.New(2)))

	// The native JSON of a map[T]struct{} is an object, not an array
	j, _ := json.Marshal(set.New("a", "b"))
	fmt.Println(string(j))

	// Output:
	// true
	// true
	// false
	// true false
	// 2 true false
	// {"a":{},"b":{}}
}
  • mapset: the full set algebra on map[K]struct{} and map[K]bool sets.
  • types.Set[T] in the root package: set type with methods and JSON support.
  • strutil.StringSet in strutil, uu.IDSet in uu and email.AddressSet in email: set types for strings, UUIDs and email addresses.
  • Sets in go-types: which set type to use.

Documentation

Overview

Package set provides generic helper functions for sets represented as plain map[T]struct{} values with T constrained to cmp.Ordered.

The package defines no set type. New creates a set, Add adds values and allocates a nil set first, and Contains, ContainsAll, ContainsAny, ContainsAllOther and ContainsAnyOther test membership. All functions accept a nil set as the empty set. Values of defined map types like the root package's types.Set can be passed directly.

New code should use package github.com/domonda/go-types/mapset, which has the full set algebra, supports map[K]bool sets and any comparable element type, and mirrors the container/mapset package proposed for Go. This package is kept unchanged for its existing callers.

See the README for the function table and a migration table to mapset.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[T cmp.Ordered](set map[T]struct{}, values ...T) map[T]struct{}

Add adds the passed values to the set and returns the set. If the passed set is nil, a new set is created.

func Contains

func Contains[T cmp.Ordered](set map[T]struct{}, value T) bool

Contains returns true if the set contains the passed value.

func ContainsAll

func ContainsAll[T cmp.Ordered](set map[T]struct{}, values ...T) bool

ContainsAll returns true if the set contains all of the passed values.

func ContainsAllOther

func ContainsAllOther[T cmp.Ordered](set map[T]struct{}, other map[T]struct{}) bool

ContainsAllOther returns true if the set contains all of the values in the other set.

func ContainsAny

func ContainsAny[T cmp.Ordered](set map[T]struct{}, values ...T) bool

ContainsAny returns true if the set contains any of the passed values.

func ContainsAnyOther

func ContainsAnyOther[T cmp.Ordered](set map[T]struct{}, other map[T]struct{}) bool

ContainsAnyOther returns true if the set contains any of the values in the other set.

func New

func New[T cmp.Ordered](values ...T) map[T]struct{}

New creates a new set with the passed values.

Types

This section is empty.

Jump to

Keyboard shortcuts

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