tsmap

package
v1.72.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package tsmap provides a collection of thread-safe map functions that can be safely used between multiple goroutines.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[K comparable, V any](mux threadsafe.RLocker, m map[K]V, key K) V

Get is a thread-safe function to get a value by key in a map.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]string{0: "Hello", 1: "World"}
	fmt.Println(tsmap.Get(mux, m, 0))
	fmt.Println(tsmap.Get(mux, m, 1))

}
Output:

Hello
World

func Len

func Len[K comparable, V any](mux threadsafe.RLocker, m map[K]V) int

Len is a thread-safe function to get the length of a map.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]string{0: "Hello", 1: "World"}
	fmt.Println(tsmap.Len(mux, m))

}
Output:

2

func Set

func Set[K comparable, V any](mux threadsafe.Locker, m map[K]V, key K, value V)

Set is a thread-safe function to assign a value to a key in a map.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.Mutex{}

	m := make(map[int]string, 2)
	tsmap.Set(mux, m, 0, "Hello")
	tsmap.Set(mux, m, 1, "World")

	fmt.Println(m)

}
Output:

map[0:Hello 1:World]

Types

This section is empty.

Jump to

Keyboard shortcuts

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