concurrent map Circle CI

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 (

go get ""

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"

For more examples have a look at concurrent_map_test.go.

Running tests:

go test ""


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)

Expand ▾ Collapse ▴




This section is empty.


View Source
var SHARD_COUNT = 32


This section is empty.


type ConcurrentMap

type ConcurrentMap []*ConcurrentMapShared

    A "thread" safe map of type string:Anything. To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.

    func New

    func New() ConcurrentMap

      Creates a new concurrent map.

      func (ConcurrentMap) Count

      func (m ConcurrentMap) Count() int

        Returns the number of elements within the map.

        func (ConcurrentMap) Get

        func (m ConcurrentMap) Get(key string) (interface{}, bool)

          Retrieves an element from map under given key.

          func (ConcurrentMap) GetShard

          func (m ConcurrentMap) GetShard(key string) *ConcurrentMapShared

            Returns shard under given key

            func (*ConcurrentMap) Has

            func (m *ConcurrentMap) Has(key string) bool

              Looks up an item under specified key

              func (*ConcurrentMap) IsEmpty

              func (m *ConcurrentMap) IsEmpty() bool

                Checks if map is empty.

                func (ConcurrentMap) Items

                func (m ConcurrentMap) Items() map[string]interface{}

                  Returns all items as map[string]interface{}

                  func (ConcurrentMap) Iter

                  func (m ConcurrentMap) Iter() <-chan Tuple

                    Returns an iterator which could be used in a for range loop.

                    Deprecated: using IterBuffered() will get a better performence

                    func (ConcurrentMap) IterBuffered

                    func (m ConcurrentMap) IterBuffered() <-chan Tuple

                      Returns a buffered iterator which could be used in a for range loop.

                      func (ConcurrentMap) Keys

                      func (m ConcurrentMap) Keys() []string

                        Return all keys as []string

                        func (ConcurrentMap) MSet

                        func (m ConcurrentMap) MSet(data map[string]interface{})

                        func (ConcurrentMap) MarshalJSON

                        func (m ConcurrentMap) MarshalJSON() ([]byte, error)

                          Reviles ConcurrentMap "private" variables to json marshal.

                          func (*ConcurrentMap) Remove

                          func (m *ConcurrentMap) Remove(key string)

                            Removes an element from the map.

                            func (*ConcurrentMap) Set

                            func (m *ConcurrentMap) Set(key string, value interface{})

                              Sets the given value under the specified key.

                              func (*ConcurrentMap) SetIfAbsent

                              func (m *ConcurrentMap) SetIfAbsent(key string, value interface{}) bool

                                Sets the given value under the specified key if no value was associated with it.

                                type ConcurrentMapShared

                                type ConcurrentMapShared struct {
                                	sync.RWMutex // Read Write mutex, guards access to internal map.
                                	// contains filtered or unexported fields

                                  A "thread" safe string to anything map.

                                  type Tuple

                                  type Tuple struct {
                                  	Key string
                                  	Val interface{}

                                    Used by the Iter & IterBuffered functions to wrap two variables together over a channel,