Skip to content
/ syncmap Public

A simple and generic Go map that is safe for concurrent use.

License

Notifications You must be signed in to change notification settings

mdawar/syncmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

syncmap

Go Reference Go Report Card Tests

A simple and generic Go map that is safe for concurrent use.

Installation

go get -u github.com/mdawar/syncmap

Usage

// Create a map that is safe for concurrent use.
m := syncmap.New[string, int]()

// Create.
m.Set("a", 1)
m.Set("b", 2)

// Get stored value.
v, ok := m.Get("a")
fmt.Println(v)  // 1
fmt.Println(ok) // true

// Delete.
m.Delete("b")

// Map length.
m.Len()

// Clear the map (Remove all entries).
m.Clear()

// Iteration.
for k, v := range m.All() {
  fmt.Println("Key", k, "/", "Value", v)
}
// Create a map with an initial capacity hint.
m := syncmap.NewWithCapacity[string, int](10_000)

// Equivalent to.
make(map[string]int, 10_000)

Tests

go test -race -cover -vet=all
# If you have "just" installed.
just test
# Or using make.
make test