Documentation
¶
Overview ¶
Collection objects such as **Map**, **Set**, etc.
Index ¶
- type BiMap
- func (self *BiMap[K, V]) DeleteValue(value V) bool
- func (self *BiMap[K, V]) GetKey(value V) (K, bool)
- func (self *BiMap[K, V]) GoString() string
- func (self *BiMap[K, V]) HasValue(value V) bool
- func (self *BiMap[K, V]) Set(key K, value V) *BiMap[K, V]
- func (self *BiMap[K, V]) String() string
- func (self *BiMap[K, V]) UnmarshalJSON(data []byte) error
- type CiMap
- func (self *CiMap[K, V]) Clear()
- func (self *CiMap[K, V]) Delete(key K) bool
- func (self *CiMap[K, V]) Entries() <-chan MapEntry[K, V]
- func (self *CiMap[K, V]) ForEach(fn func(value V, key K))
- func (self *CiMap[K, V]) Get(key K) (V, bool)
- func (self *CiMap[K, V]) GetAndSet(key K, value V) (V, bool)
- func (self *CiMap[K, V]) GoString() string
- func (self *CiMap[K, V]) Has(key K) bool
- func (self *CiMap[K, V]) Keys() []K
- func (self *CiMap[K, V]) MarshalJSON() ([]byte, error)
- func (self *CiMap[K, V]) Pop(key K) (V, bool)
- func (self *CiMap[K, V]) Set(key K, value V) *CiMap[K, V]
- func (self *CiMap[K, V]) String() string
- func (self *CiMap[K, V]) ToMap() map[K]V
- func (self *CiMap[K, V]) UnmarshalJSON(data []byte) error
- func (self *CiMap[K, V]) Use(key K, init func() V) V
- type Map
- func (self *Map[K, V]) Clear()
- func (self *Map[K, V]) Delete(key K) bool
- func (self *Map[K, V]) Entries() <-chan MapEntry[K, V]
- func (self *Map[K, V]) ForEach(fn func(value V, key K))
- func (self *Map[K, V]) Get(key K) (V, bool)
- func (self *Map[K, V]) GetAndSet(key K, value V) (V, bool)
- func (self *Map[K, V]) GoString() string
- func (self *Map[K, V]) Has(key K) bool
- func (self *Map[K, V]) Keys() []K
- func (self *Map[K, V]) MarshalJSON() ([]byte, error)
- func (self *Map[K, V]) Pop(key K) (V, bool)
- func (self *Map[K, V]) Set(key K, value V) *Map[K, V]
- func (self *Map[K, V]) Size() int
- func (self *Map[K, V]) String() string
- func (self *Map[K, V]) ToMap() map[K]V
- func (self *Map[K, V]) UnmarshalJSON(data []byte) error
- func (self *Map[K, V]) Use(key K, init func() V) V
- func (self *Map[K, V]) Values() []V
- type MapEntry
- type Set
- func (self *Set[T]) Add(item T) *Set[T]
- func (self *Set[T]) Clear()
- func (self *Set[T]) Delete(item T) bool
- func (self *Set[T]) ForEach(fn func(item T))
- func (self *Set[T]) GoString() string
- func (self *Set[T]) Has(item T) bool
- func (self *Set[T]) MarshalJSON() ([]byte, error)
- func (self *Set[T]) Pop(order int) (T, bool)
- func (self *Set[T]) Random() (T, bool)
- func (self *Set[T]) Size() int
- func (self *Set[T]) String() string
- func (self *Set[T]) UnmarshalJSON(data []byte) error
- func (self *Set[T]) Values() []T
Examples ¶
- BiMap
- BiMap (Json)
- BiMap.DeleteValue
- BiMap.GetKey
- BiMap.HasValue
- BiMap.Set
- CiMap
- CiMap (Json)
- CiMap.Clear
- CiMap.Delete
- CiMap.Entries
- CiMap.ForEach
- CiMap.Get
- CiMap.GetAndSet
- CiMap.Has
- CiMap.Keys
- CiMap.Pop
- CiMap.Set
- CiMap.ToMap
- CiMap.Use
- Map
- Map (Json)
- Map.Clear
- Map.Delete
- Map.Entries
- Map.ForEach
- Map.Get
- Map.GetAndSet
- Map.Has
- Map.Keys
- Map.Pop
- Map.Set
- Map.Size
- Map.ToMap
- Map.Use
- Map.Values
- NewBiMap
- NewCiMap
- NewMap
- NewSet
- Set
- Set (Json)
- Set.Add
- Set.Clear
- Set.Delete
- Set.ForEach
- Set.Has
- Set.Pop
- Set.Random
- Set.Size
- Set.Values
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 ¶
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 ¶
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]) HasValue ¶
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 ¶
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]) UnmarshalJSON ¶
type CiMap ¶
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 ¶
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 ¶
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
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 ¶
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
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]) Has ¶
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 (*CiMap[K, V]) Pop ¶ added in v0.4.1
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 ¶
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]) 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 (*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 ¶
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
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 ¶
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
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]) Has ¶
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 (*Map[K, V]) Pop ¶ added in v0.4.1
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 ¶
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 ¶
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]) 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 (*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 ¶
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 ¶
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]) Has ¶
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 (*Set[T]) Pop ¶ added in v0.4.3
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
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 ¶
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