atomicmapper is a code generation tool for creating high-performance, scalable, frequently read, but infrequently updated maps of strings to any given type map[string]YourType
. It is based on Go's atomic.Value read mostly example.
Requires Go 1.4+
Install atomicmapper.
go install github.com/ninibe/atomicmapper
# move to $PATH if $GOPATH/bin is not in your $PATH
Add the generate command.
//go:generate atomicmapper -pointer -type Foo
type Foo struct { ... }
Skip the -pointer
flag to save entire values.
Generate the atomic map code.
go generate myfoopkg
This will create a new foo_atomicmap.go
file ready to use.
fooMap := NewFooAtomicMap()
fooMap.Set("myKey", &Foo{}) // save pointer to Foo
foo, ok := fooMap.Get("myKey") // retrieve pointer
fooMap.Delete("myKey") // remove pointer from map
All methods are thread-safe while Get
is also lock-free.
Check the example godoc