collections

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 8 Imported by: 4

Documentation

Overview

Collection objects such as **Map**, **Set**, etc.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BiMap

type BiMap[K comparable, V comparable] struct {
	Map[K, V]
}

Thread-safe bi-directional map, keys and values are unique and map to each other.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := &collections.BiMap[string, string]{} // use & for literal creation
	m.Set("foo", "Hello").Set("bar", "World")

	fmt.Println(m)
}
Output:

&collections.BiMap[foo:Hello bar:World]
Example (Json)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := &collections.BiMap[string, string]{}
	m.Set("foo", "Hello").Set("bar", "World")

	data, _ := json.Marshal(m)
	fmt.Println(string(data))

	m2 := &collections.BiMap[string, string]{}
	json.Unmarshal(data, m2)
	fmt.Println(m2)
}
Output:

{"foo":"Hello","bar":"World"}
&collections.BiMap[bar:World foo:Hello]

func NewBiMap

func NewBiMap[K comparable, V comparable](initial []MapEntry[K, V]) *BiMap[K, V]

Creates a new instance of the BiMap.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewBiMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m)
}
Output:

&collections.BiMap[foo:Hello bar:World]

func (*BiMap[K, V]) DeleteValue

func (self *BiMap[K, V]) DeleteValue(value V) bool

Removes the key-value pair by the given value.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewBiMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	ok1 := m.DeleteValue("Hello") // succeed
	ok2 := m.DeleteValue("Hello") // failed

	fmt.Println(m)
	fmt.Println(ok1)
	fmt.Println(ok2)
}
Output:

&collections.BiMap[bar:World]
true
false

func (*BiMap[K, V]) GetKey

func (self *BiMap[K, V]) GetKey(value V) (K, bool)

Retrieves a key by the given value. If the value doesn't exist, it returns the zero-value of type `K` and `false`.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewBiMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.GetKey("Hello"))
	fmt.Println(m.GetKey("Hi"))
}
Output:

foo true
 false

func (*BiMap[K, V]) GoString

func (self *BiMap[K, V]) GoString() string

func (*BiMap[K, V]) HasValue

func (self *BiMap[K, V]) HasValue(value V) bool

Checks if the given value exists in the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewBiMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.HasValue("Hello"))
	fmt.Println(m.HasValue("Hi"))
}
Output:

true
false

func (*BiMap[K, V]) Set

func (self *BiMap[K, V]) Set(key K, value V) *BiMap[K, V]

Sets a pair of key and value in the map. If the key already exists, it changes the corresponding value; if the value already exists, it changes the corresponding key; if both are missing, it adds the new pair into the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewBiMap([]collections.MapEntry[string, string]{})
	m.Set("foo", "Hello").Set("bar", "World") // Set() method can be chained

	fmt.Println(m) // keys' order is preserved
	fmt.Printf("%#v", m)
}
Output:

&collections.BiMap[foo:Hello bar:World]
&collections.BiMap[string, string]{"foo":"Hello", "bar":"World"}

func (*BiMap[K, V]) String

func (self *BiMap[K, V]) String() string

func (*BiMap[K, V]) UnmarshalJSON

func (self *BiMap[K, V]) UnmarshalJSON(data []byte) error

type CiMap

type CiMap[K ~string, V any] struct {
	Map[K, V]
	// contains filtered or unexported fields
}

Thread-safe case-insensitive map, keys are case-insensitive.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := &collections.CiMap[string, string]{} // use & for literal creation
	m.Set("Foo", "Hello").Set("bar", "World")

	fmt.Println(m)
}
Output:

&collections.CiMap[Foo:Hello bar:World]
Example (Json)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := &collections.CiMap[string, string]{}
	m.Set("Foo", "Hello").Set("bar", "World")

	data, _ := json.Marshal(m)
	fmt.Println(string(data))

	m2 := &collections.CiMap[string, string]{}
	json.Unmarshal(data, m2)
	fmt.Println(m2)
}
Output:

{"Foo":"Hello","bar":"World"}
&collections.CiMap[Foo:Hello bar:World]

func NewCiMap

func NewCiMap[K ~string, V string](initial []MapEntry[K, V]) *CiMap[K, V]

Creates a new instance of the CiMap.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m)
}
Output:

&collections.CiMap[Foo:Hello bar:World]

func (*CiMap[K, V]) Clear

func (self *CiMap[K, V]) Clear()
Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	m.Clear()

	fmt.Println(m)
}
Output:

&collections.CiMap[]

func (*CiMap[K, V]) Delete

func (self *CiMap[K, V]) Delete(key K) bool

Removes the key-value pair by the given key.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	ok1 := m.Delete("foo") // succeed
	ok2 := m.Delete("foo") // failed

	fmt.Println(m)
	fmt.Println(ok1)
	fmt.Println(ok2)
}
Output:

&collections.CiMap[bar:World]
true
false

func (*CiMap[K, V]) Entries added in v0.3.6

func (self *CiMap[K, V]) Entries() <-chan MapEntry[K, V]

Returns a channel for the map entries that can be used in the `for...range...` loop.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	for entry := range m.Entries() {
		fmt.Println(entry.Key, "=>", entry.Value)
	}
}
Output:

Foo => Hello
bar => World

func (*CiMap[K, V]) ForEach

func (self *CiMap[K, V]) ForEach(fn func(value V, key K))

Loop through all the key-value pairs in the map and invoke the given function against them.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	m.ForEach(func(value string, key string) {
		fmt.Println(key, "=>", value)
	})
}
Output:

Foo => Hello
bar => World

func (*CiMap[K, V]) Get

func (self *CiMap[K, V]) Get(key K) (V, bool)

Retrieves a value by the given key. If the key doesn't exist, it returns the zero-value of type `V` and `false`.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Get("foo"))
	fmt.Println(m.Get("foo1"))
}
Output:

Hello true
 false

func (*CiMap[K, V]) GetAndSet added in v0.4.2

func (self *CiMap[K, V]) GetAndSet(key K, value V) (V, bool)

Retrieves the previous value by the given key and set a new value.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	val1, _ := m.GetAndSet("Foo", "Hi")
	val2, _ := m.Get("foo")

	fmt.Println(val1)
	fmt.Println(val2)
}
Output:

Hello
Hi

func (*CiMap[K, V]) GoString

func (self *CiMap[K, V]) GoString() string

func (*CiMap[K, V]) Has

func (self *CiMap[K, V]) Has(key K) bool

Checks if the given key exists in the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Has("foo"))
	fmt.Println(m.Has("foo1"))
}
Output:

true
false

func (*CiMap[K, V]) Keys

func (self *CiMap[K, V]) Keys() []K

Retrieves all the keys in the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Keys()) // keys' names and their order are preserved
}
Output:

[Foo bar]

func (*CiMap[K, V]) MarshalJSON

func (self *CiMap[K, V]) MarshalJSON() ([]byte, error)

func (*CiMap[K, V]) Pop added in v0.4.1

func (self *CiMap[K, V]) Pop(key K) (V, bool)

Removes and returns the key-value pair by the given key.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Pop("foo"))
	fmt.Println(m.Has("foo"))
	fmt.Println(m.Pop("bar1"))
}
Output:

Hello true
false
 false

func (*CiMap[K, V]) Set

func (self *CiMap[K, V]) Set(key K, value V) *CiMap[K, V]

Sets a pair of key and value in the map. If the key already exists, it changes the corresponding value; otherwise, it adds the new pair into the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := &collections.CiMap[string, string]{}
	m.Set("Foo", "Hello").Set("bar", "World") // Set() method can be chained

	fmt.Println(m) // keys' names and their order are preserved
	fmt.Printf("%#v\n", m)
}
Output:

&collections.CiMap[Foo:Hello bar:World]
&collections.CiMap[string, string]{"Foo":"Hello", "bar":"World"}

func (*CiMap[K, V]) String

func (self *CiMap[K, V]) String() string

func (*CiMap[K, V]) ToMap

func (self *CiMap[K, V]) ToMap() map[K]V

Creates a builtin `map` based on this map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"Foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.ToMap()) // the printed representation is ordered alphabetically, but the real value is not
}
Output:

map[Foo:Hello bar:World]

func (*CiMap[K, V]) UnmarshalJSON

func (self *CiMap[K, V]) UnmarshalJSON(data []byte) error

func (*CiMap[K, V]) Use added in v0.4.2

func (self *CiMap[K, V]) Use(key K, init func() V) V

Retrieves a value by the given key. If the key doesn't exist yet, invokes the `init` function for setting the value and return it.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewCiMap([]collections.MapEntry[string, string]{
		{"foo", "Hi"},
	})

	val1 := m.Use("Foo", func() string { return "Hello" })
	val2 := m.Use("Bar", func() string { return "World" })

	fmt.Println(val1)
	fmt.Println(val2)
}
Output:

Hi
World

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map is an object-oriented collection of map with ordered keys and thread-safe by default.

Unlike the builtin `map` type, this Map stores data in a underlying list, which provides ordered keys sequence. However, the op time is O(n), which might be inefficient for large amount of data. Use with caution.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := &collections.Map[string, string]{} // use & for literal creation
	m.Set("foo", "Hello").Set("bar", "World")

	fmt.Println(m)
}
Output:

&collections.Map[foo:Hello bar:World]
Example (Json)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := &collections.Map[string, string]{}
	m.Set("foo", "Hello").Set("bar", "World")

	data, _ := json.Marshal(m)
	fmt.Println(string(data))

	m2 := &collections.Map[string, string]{}
	json.Unmarshal(data, m2)
	fmt.Println(m2)
}
Output:

{"foo":"Hello","bar":"World"}
&collections.Map[bar:World foo:Hello]

func NewMap

func NewMap[K comparable, V any](initial []MapEntry[K, V]) *Map[K, V]

Creates a new instance of the Map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m)
}
Output:

&collections.Map[foo:Hello bar:World]

func (*Map[K, V]) Clear

func (self *Map[K, V]) Clear()

Empties the map and resets its size.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	m.Clear()

	fmt.Println(m)
}
Output:

&collections.Map[]

func (*Map[K, V]) Delete

func (self *Map[K, V]) Delete(key K) bool

Removes the key-value pair by the given key.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	ok1 := m.Delete("foo") // succeed
	ok2 := m.Delete("foo") // fail

	fmt.Println(m)
	fmt.Println(ok1)
	fmt.Println(ok2)
}
Output:

&collections.Map[bar:World]
true
false

func (*Map[K, V]) Entries added in v0.3.6

func (self *Map[K, V]) Entries() <-chan MapEntry[K, V]

Returns a channel for the map entries that can be used in the `for...range...` loop.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	for entry := range m.Entries() {
		fmt.Println(entry.Key, "=>", entry.Value)
	}
}
Output:

foo => Hello
bar => World

func (*Map[K, V]) ForEach

func (self *Map[K, V]) ForEach(fn func(value V, key K))

Loop through all the key-value pairs in the map and invoke the given function against them.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	m.ForEach(func(value string, key string) {
		fmt.Println(key, "=>", value)
	})
}
Output:

foo => Hello
bar => World

func (*Map[K, V]) Get

func (self *Map[K, V]) Get(key K) (V, bool)

Retrieves a value by the given key. If the key doesn't exist, it returns the zero-value of type `V` and `false`.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Get("foo"))
	fmt.Println(m.Get("foo1"))
}
Output:

Hello true
 false

func (*Map[K, V]) GetAndSet added in v0.4.2

func (self *Map[K, V]) GetAndSet(key K, value V) (V, bool)

Retrieves the previous value by the given key and set a new value.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	val1, _ := m.GetAndSet("foo", "Hi")
	val2, _ := m.Get("foo")

	fmt.Println(val1)
	fmt.Println(val2)
}
Output:

Hello
Hi

func (*Map[K, V]) GoString

func (self *Map[K, V]) GoString() string

func (*Map[K, V]) Has

func (self *Map[K, V]) Has(key K) bool

Checks if the given key exists in the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Has("foo"))
	fmt.Println(m.Has("foo1"))
}
Output:

true
false

func (*Map[K, V]) Keys

func (self *Map[K, V]) Keys() []K

Retrieves all the keys in the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Keys()) // keys' order is preserved
}
Output:

[foo bar]

func (*Map[K, V]) MarshalJSON

func (self *Map[K, V]) MarshalJSON() ([]byte, error)

func (*Map[K, V]) Pop added in v0.4.1

func (self *Map[K, V]) Pop(key K) (V, bool)

Removes and returns the key-value pair by the given key.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Pop("foo"))
	fmt.Println(m.Has("foo"))
	fmt.Println(m.Pop("bar1"))
}
Output:

Hello true
false
 false

func (*Map[K, V]) Set

func (self *Map[K, V]) Set(key K, value V) *Map[K, V]

Sets a pair of key and value in the map. If the key already exists, it changes the corresponding value; otherwise, it adds the new pair into the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{})
	m.Set("foo", "Hello").Set("bar", "World") // Set() method can be chained

	fmt.Println(m) // keys' order is preserved
	fmt.Printf("%#v\n", m)
}
Output:

&collections.Map[foo:Hello bar:World]
&collections.Map[string, string]{"foo":"Hello", "bar":"World"}

func (*Map[K, V]) Size

func (self *Map[K, V]) Size() int

Returns the size of the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{})
	fmt.Println(m.Size())

	m.Set("foo", "Hello")
	fmt.Println(m.Size())

	m.Set("bar", "World")
	fmt.Println(m.Size())
}
Output:

0
1
2

func (*Map[K, V]) String

func (self *Map[K, V]) String() string

func (*Map[K, V]) ToMap

func (self *Map[K, V]) ToMap() map[K]V

Creates a builtin `map` based on this map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.ToMap()) // the printed representation is ordered alphabetically, but the real value is not
}
Output:

map[bar:World foo:Hello]

func (*Map[K, V]) UnmarshalJSON

func (self *Map[K, V]) UnmarshalJSON(data []byte) error

func (*Map[K, V]) Use added in v0.4.2

func (self *Map[K, V]) Use(key K, init func() V) V

Retrieves a value by the given key. If the key doesn't exist yet, invokes the `init` function for setting the value and return it.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hi"},
	})

	val1 := m.Use("foo", func() string { return "Hello" })
	val2 := m.Use("bar", func() string { return "World" })

	fmt.Println(val1)
	fmt.Println(val2)
}
Output:

Hi
World

func (*Map[K, V]) Values

func (self *Map[K, V]) Values() []V

Retrieves all the values in the map.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	m := collections.NewMap([]collections.MapEntry[string, string]{
		{"foo", "Hello"},
		{"bar", "World"},
	})

	fmt.Println(m.Values()) // values' order is the same as keys'
}
Output:

[Hello World]

type MapEntry added in v0.3.6

type MapEntry[K comparable, V any] struct {
	Key   K
	Value V
}

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is an object-oriented collection that stores unique items and is thread-safe.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := &collections.Set[string]{} // use & for literal creation
	s.Add("Hello").Add("World")

	fmt.Println(s)
}
Output:

&collections.Set[Hello World]
Example (Json)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := &collections.Set[string]{} // use & for literal creation
	s.Add("Hello").Add("World")

	data, _ := json.Marshal(s)
	fmt.Println(string(data))

	s2 := &collections.Set[string]{}
	json.Unmarshal(data, s2)
	fmt.Printf("%#v", s2)
}
Output:

["Hello","World"]
&collections.Set[string]{"Hello", "World"}

func NewSet

func NewSet[T comparable](base []T) *Set[T]

Creates a new instance of the Set.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{
		"Hello",
		"World",
		"Hello",
		"A-yon",
	})

	fmt.Println(s)
}
Output:

&collections.Set[Hello World A-yon]

func (*Set[T]) Add

func (self *Set[T]) Add(item T) *Set[T]

Adds an item to the set. If the item already exists, the set remains untouched.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{})

	s.Add("Hello").Add("World") // Add() method can be chained
	s.Add("Hello")              // duplicate adding values will not effect

	fmt.Println(s)
}
Output:

&collections.Set[Hello World]

func (*Set[T]) Clear

func (self *Set[T]) Clear()

Empties the set and resets its size.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{"Hello", "World"})

	s.Clear()

	fmt.Println(s)
}
Output:

&collections.Set[]

func (*Set[T]) Delete

func (self *Set[T]) Delete(item T) bool

Removes the item from the set.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{"Hello", "World"})

	ok1 := s.Delete("Hello") // succeed
	ok2 := s.Delete("Hello") // failed

	fmt.Println(s)
	fmt.Println(ok1)
	fmt.Println(ok2)
}
Output:

&collections.Set[World]
true
false

func (*Set[T]) ForEach

func (self *Set[T]) ForEach(fn func(item T))

Loop through all the items in the set and invoke the given function against them.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{"Hello", "World"})

	s.ForEach(func(item string) {
		fmt.Println(item)
	})
}
Output:

Hello
World

func (*Set[T]) GoString

func (self *Set[T]) GoString() string

func (*Set[T]) Has

func (self *Set[T]) Has(item T) bool

Checks if the given item exists in the set.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{"Hello"})

	fmt.Println(s.Has("Hello"))
	fmt.Println(s.Has("World"))
}
Output:

true
false

func (*Set[T]) MarshalJSON

func (self *Set[T]) MarshalJSON() ([]byte, error)

func (*Set[T]) Pop added in v0.4.3

func (self *Set[T]) Pop(order int) (T, bool)

Removes and returns the first (if `order >= 0`) or the last (if `order < 0`) item from the set.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{
		"Hello",
		"World",
		"Hi",
		"A-yon",
	})

	fmt.Println(s.Pop(0))
	fmt.Println(s.Pop(-1))
	fmt.Println(s.Size())

	s.Clear()
	fmt.Println(s.Pop(0))

}
Output:

Hello true
A-yon true
2
 false

func (*Set[T]) Random added in v0.4.3

func (self *Set[T]) Random() (T, bool)

Removes and returns a random item from the set.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/ayonli/goext/collections"
)

func main() {
	list := []string{
		"Hello",
		"World",
		"Hi",
		"A-yon",
	}
	s := collections.NewSet(list)

	item, _ := s.Random()
	fmt.Println(slices.Contains(list, item))
	fmt.Println(s.Size())

	s.Clear()
	fmt.Println(s.Random())

}
Output:

true
3
 false

func (*Set[T]) Size

func (self *Set[T]) Size() int

Returns the size of the set.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{"Hello", "World"})

	fmt.Println(s.Size())
}
Output:

2

func (*Set[T]) String

func (self *Set[T]) String() string

func (*Set[T]) UnmarshalJSON

func (self *Set[T]) UnmarshalJSON(data []byte) error

func (*Set[T]) Values

func (self *Set[T]) Values() []T

Retrieves all the values in the set.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/collections"
)

func main() {
	s := collections.NewSet([]string{"Hello", "World"})

	fmt.Println(s.Values())
}
Output:

[Hello World]

Jump to

Keyboard shortcuts

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