Skip to content
This repository has been archived by the owner. It is now read-only.
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


No Maintenance Intended


This project has been archived. No future work will be done here.

Build Status Code Climate Issue Count Go Report Card Apache V2 License GitHub release GoDoc


Capacityset implements a set (unordered, non-repeating) that cannot exceed a provisioned size.

Table of Contents

Code of Conduct

This project and everyone participating in it are governed by the XMiDT Code Of Conduct. By participating, you agree to this Code.


This repo is a package. To install it, just run the go get command from your command line:

go get -u


Creating a Set and adding values is very easy. You can not add duplicates to the set:

s := capacityset.NewCapacitySet(3)
// the Add method returns a boolean indicating if the operation was successful
s.Add("banana") // true
s.Add("peach") // true
fmt.Println(s.Add("banana")) // prints false because the value is already present in the Set

In order to retrieve values from the Set, you can use the Pop function. In addition to that, the Size function helps you to keep track of the current size of the Set:

s := capacityset.NewCapacitySet(3)
s.Add("banana") // true
s.Add("peach") // true
s.Add("apple") // true
fmt.Println(s.Size()) // prints 3
// the Pop method returns a random element and removes it from the Set
fmt.Println(s.Pop()) // either banana, peach or apple
fmt.Println(s.Size()) // prints 2

The Set implementation is designed to work concurrently from multiple Goroutines. Therefore, if you try to add an element to a full Set, the Add function will block as long as the Set is full:

s := capacityset.NewCapacitySet(3)
s.Add("banana") // true
s.Add("peach") // true
s.Add("apple") // true
go func() {
    time.Sleep(time.Second*3) // simulates work until the Pop function is called
    s.Pop() // either banana, peach or apple
before := time.Now()
s.Add("coconut") // blocks until the set is no longer full
fmt.Println(time.Since(before)) // would print round about "3s"

Please note that if you try to add an element to a full Set within the same Goroutine, the Add function panics because Golang detects a deadlock.


Refer to


Library that implements a set with a maximum size





No packages published

Contributors 4