safe

package
v1.0.25 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Package safe provides a thread-safe map implementation in Go.

The Map struct in this package is a generic map that can hold any type of comparable keys and any type of values. It uses a sync.RWMutex to ensure that it can be safely used from multiple goroutines.

The NewMap function returns a new instance of a Map.

The Set method on a Map sets the value for a key, acquiring the lock before doing so to ensure thread safety.

The SetIfNotExists method on a Map sets the value for a key only if the key does not already exist in the map, acquiring the lock before doing so to ensure thread safety. It returns true if the value was set, and false otherwise.

Index

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 {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewMap

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

func (*Map[K, V]) Delete

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

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(k K) (V, bool)

func (*Map[K, V]) Has

func (m *Map[K, V]) Has(k K) bool

func (*Map[K, v]) Keys added in v1.0.12

func (m *Map[K, v]) Keys() []K

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(k K, v V)

func (*Map[K, V]) SetIfNotExists added in v1.0.6

func (m *Map[K, V]) SetIfNotExists(k K, v V) bool

type UnlimitedChannel added in v1.0.18

type UnlimitedChannel[T any] struct {
	// contains filtered or unexported fields
}

func NewUnlimitedChannel added in v1.0.18

func NewUnlimitedChannel[T any]() *UnlimitedChannel[T]

NewUnlimitedChannel creates a new instance of UnlimitedChannel[T]. It initializes the necessary channels and storage for the channel. It also starts a goroutine to manage the channel's operations.

func (*UnlimitedChannel[T]) Finalize added in v1.0.18

func (c *UnlimitedChannel[T]) Finalize() []T

Finalize closes the channel and waits for the finalization to happen. It returns the storage of the channel. Note: Write to In() won't block even after finalize, but it won't be effective also

func (*UnlimitedChannel[T]) In added in v1.0.18

func (c *UnlimitedChannel[T]) In() chan T

In returns the input channel of the UnlimitedChannel.

func (*UnlimitedChannel[T]) Out added in v1.0.18

func (c *UnlimitedChannel[T]) Out() <-chan T

Out returns a receive-only channel of type T. This channel can be used to receive values sent to the UnlimitedChannel.

Jump to

Keyboard shortcuts

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