Documentation
¶
Overview ¶
Package syncmap provides a simple and generic map that is safe for concurrent use.
Example ¶
package main
import (
"fmt"
"github.com/mdawar/syncmap"
)
func main() {
m := syncmap.New[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Delete("b")
fmt.Println(m.Len())
fmt.Println(m.Get("a"))
fmt.Println(m.Get("b"))
m.Clear()
fmt.Println(m.Len())
}
Output: 1 1 true 0 false 0
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a map that is safe for concurrent use.
func New ¶
func New[K comparable, V any]() *Map[K, V]
New creates a new Map that is safe for concurrent use by multiple goroutines.
func NewWithCapacity ¶
func NewWithCapacity[K comparable, V any](capacity int) *Map[K, V]
NewWithCapacity creates a new Map with the specified capacity hint.
The capacity hint does not bound the map size, it will create a map with an initial space to hold the specified number of elements.
Example ¶
package main
import (
"fmt"
"github.com/mdawar/syncmap"
)
func main() {
// Create a map with a capacity hint.
m := syncmap.NewWithCapacity[string, int](10_000)
m.Set("a", 1)
fmt.Println(m.Len())
fmt.Println(m.Get("a"))
fmt.Println(m.Get("b"))
}
Output: 1 1 true 0 false
func (*Map[K, V]) All ¶
All returns an iterator over key-value pairs from the Map.
Similar to the map type, the iteration order is not guaranteed.
Example ¶
package main
import (
"fmt"
"github.com/mdawar/syncmap"
)
func main() {
m := syncmap.New[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
for k, v := range m.All() {
fmt.Println("Key:", k, "-", "Value:", v)
}
}
Output: Key: a - Value: 1 Key: b - Value: 2 Key: c - Value: 3
func (*Map[K, V]) Delete ¶
func (m *Map[K, V]) Delete(key K)
Delete deletes the value for a key in the Map.
Click to show internal directories.
Click to hide internal directories.