tsmap

package
v1.76.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 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[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M, k K) V

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

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[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M) int

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

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[M ~map[K]V, K comparable, V any](mux threadsafe.Locker, m M, k K, v V)

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

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