As explained here and here, the map
type in Go doesn't support concurrent reads and writes. concurrent-map
provides a high-performance solution to this by sharding the map with minimal time spent waiting for locks.
Import the package:
import (
"github.com/streamrail/concurrent-map"
)
go get "github.com/streamrail/concurrent-map"
The package is now imported under the "cmap" namespace.
// Create a new map.
map := cmap.New()
// Sets item within map, sets "bar" under key "foo"
map.Set("foo", "bar")
// Retrieve item from map.
if tmp, ok := map.Get("foo"); ok {
bar := tmp.(string)
}
// Removes item under key "foo"
map.Remove("foo")
For more examples have a look at concurrent_map_test.go.
Running tests:
go test "github.com/streamrail/concurrent-map"
To generate your own custom concurrent maps please use concurrent_map_template.txt, the file is a base template for type specific maps. For Example to create a new go source file for a string:int map, in terminal run:
sed 's/\<KEY\>/string/g;s/\<VAL\>/int/g' concurrent_map_template.txt > cmap_string_int.go
You can change the string and the int in the sed command to whatever you need.
MIT (see LICENSE file)