It's modern package that based on generic maps. The package provides many helpers to operate on thread-safe (only) maps. You don't have to worry about the concurrency moment of this map. Under the hood, realization is based on sync.Map.
go get github.com/0x9ef/go-maps
import "github.com/0x9ef/go-maps"We can start from the native map realisation.
We can set map key with a value.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1) // sets for key "one" value "1" We can set map key with a value if predicate function is true.
m := maps.NewDefaultMap[string, int]()
m.Set("ten", 10)
m.SetIf("one", 1, func(m Map[int, int]) bool {
return m.Get("ten") == 10 // sets only if "ten" key is equals to 10
})We can get a map value.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
val := m.Get("one")
if val == 1 {
fmt.Println("found")
}We can get a value and identifier if a record was found.
m := maps.NewDefaultMap[string, int]()
val, ok := m.GetOk("one")
if !ok {
fmt.Println("key was not found")
} We can get a value or if value was not found we have to store it.
m := maps.NewDefaultMap[string, int]()
val, loaded := m.GetOrSet("one", 1)
if !loaded {
fmt.Println("key was not found, but we store it")
}
fmt.Println(val)
// 1We can get a value and after this delete this value from the map.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
val, loaded := m.GetAndDelete("one")
if !loaded {
fmt.Println("key was not found, we didn't delete it")
}
fmt.Println(val)
// 1 We can delete a key from the map.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Delete("one")We can delete a key from the map if predicate function is true.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.DeleteIf("one", func(m Map[int, int]) bool {
return !m.Exists("ten") // deletes value only if key "ten" doesn't exists
})We can clear full map, so this means that we can delete all keys from the map.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
m.Clear()We can get all keys from the map.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
fmt.Println(m.Keys())
// ["one", "two", "three"]We can get all values from the map.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
fmt.Println(m.Values())
// [1, 2, 3]We can filter map content and return only keys and values that was matched by our rules.
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
m.Set("four", 3)
keys, values := m.Filter(func(key string, value int) bool {
return key == "three" && value == 3 // we match third element in the map
})
fmt.Println(keys, values)
// ["three"], [1]We can iterate over all keys in the map
m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
m.Iterate(func(key string, value int) bool{
fmt.Println(key, value)
})
// "one" 1
// "two" 2
// "three" 3