Documentation
¶
Overview ¶
Package set is a set package provides set data structure for Go. It is written without any dependency.
package main
import (
"fmt"
"github.com/gozeloglu/set"
)
func main() {
s := set.New()
s.Add(123)
exist := s.Contains(123)
if !exist {
fmt.Println("123 not exist")
}
s.Append(1, 2, 3, 4, "abc") // Add multiple values
values := []interface{}{"github", 100, 640, 0.43, false}
s.Append(values...) // Append the array of elements
s.Remove(4)
size := s.Size()
fmt.Println(size) // Prints 5
s.Pop() // Returns random value from the set
s.Clear()
fmt.Println(s.Size()) // Prints 0
}
You can call the Union() method for creating a new Set which contains the all items from the set1 and set2. It concatenates two sets and creates a new one.
union := set1.Union(set2)
In order to take intersection of the sets, you can call the Intersection() method.
intersect := set1.Intersection(set2)
Index ¶
- type S
- type Set
- func (s *Set) Add(val interface{})
- func (s *Set) Append(values ...interface{})
- func (s *Set) Clear()
- func (s Set) Contains(val interface{}) bool
- func (s Set) Empty() bool
- func (s *Set) Intersection(set *Set) *Set
- func (s Set) Pop() interface{}
- func (s *Set) Remove(val interface{})
- func (s Set) Size() uint
- func (s Set) Slice() []interface{}
- func (s Set) Union(set *Set) *Set
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type S ¶
type S interface {
Add(val interface{})
Append(val ...interface{})
Remove(val interface{})
Contains(val interface{}) bool
Size() uint
Pop() interface{}
Clear()
Empty() bool
Slice() []interface{}
Union(set *Set) *Set
Intersection(set *Set) *Set
}
S is set interface.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is the data structure which provides some functionalities.
func (*Set) Add ¶
func (s *Set) Add(val interface{})
Add adds a new values to set if there is enough capacity.
func (*Set) Append ¶
func (s *Set) Append(values ...interface{})
Append adds multiple values into set.
func (*Set) Intersection ¶ added in v0.3.0
Intersection takes the common values from both sets and returns a new set that stores the common ones.
func (Set) Pop ¶
func (s Set) Pop() interface{}
Pop returns a random value from the set. If there is no element in set, it returns nil.