syncmap

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2024 License: MIT Imports: 2 Imported by: 0

README

syncmap

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

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)

Documentation

Overview

Package syncmap provides a simple and generic map that is safe for concurrent use.

Example
package main

import (
	"fmt"

	"github.com/mdawar/syncmap"
)

func main() {
	m := syncmap.New[string, int]()

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

	m.Delete("b")

	fmt.Println(m.Len())
	fmt.Println(m.Get("a"))
	fmt.Println(m.Get("b"))

	m.Clear()

	fmt.Println(m.Len())

}
Output:

1
1 true
0 false
0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map is a map that is safe for concurrent use.

func New

func New[K comparable, V any]() *Map[K, V]

New creates a new Map that is safe for concurrent use by multiple goroutines.

func NewWithCapacity

func NewWithCapacity[K comparable, V any](capacity int) *Map[K, V]

NewWithCapacity creates a new Map with the specified capacity hint.

The capacity hint does not bound the map size, it will create a map with an initial space to hold the specified number of elements.

Example
package main

import (
	"fmt"

	"github.com/mdawar/syncmap"
)

func main() {
	// Create a map with a capacity hint.
	m := syncmap.NewWithCapacity[string, int](10_000)

	m.Set("a", 1)

	fmt.Println(m.Len())
	fmt.Println(m.Get("a"))
	fmt.Println(m.Get("b"))

}
Output:

1
1 true
0 false

func (*Map[K, V]) All

func (m *Map[K, V]) All() iter.Seq2[K, V]

All returns an iterator over key-value pairs from the Map.

Similar to the map type, the iteration order is not guaranteed.

Example
package main

import (
	"fmt"

	"github.com/mdawar/syncmap"
)

func main() {
	m := syncmap.New[string, int]()

	m.Set("a", 1)
	m.Set("b", 2)
	m.Set("c", 3)

	for k, v := range m.All() {
		fmt.Println("Key:", k, "-", "Value:", v)
	}

}
Output:

Key: a - Value: 1
Key: b - Value: 2
Key: c - Value: 3

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

Clear deletes all the entries in the Map.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete deletes the value for a key in the Map.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (value V, ok bool)

Get returns the value stored in the Map for a key, or the zero value if no value is present.

The ok result indicates whether the value was found in the map.

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the length of the Map.

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(key K, value V)

Set sets the value for a key in the Map.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL