gmap

package
v0.0.0-...-1e3dbca Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2025 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns the keys of a map as a slice

func Map

func Map[K comparable, V any, R any](m map[K]V, f func(K, V) R) []R

Map iterate through a map and generate a R[]

func ShallowCopy

func ShallowCopy[K comparable, V any](m map[K]V) map[K]V

func SortedKeys

func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K
Example
m := map[int]string{}
m[1] = "John"
m[5] = "Marry"
m[9] = "Rose"

// get the sorted keys
keys := SortedKeys(m)

// print the sorted keys
fmt.Println(keys)
Output:

[1 5 9]

func Values

func Values[K comparable, V any](m map[K]V) []V

Types

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

func NewPair

func NewPair[K comparable, V any](k K, v V) Pair[K, V]

func ToSlice

func ToSlice[K comparable, V any](m map[K]V) []Pair[K, V]

ToSlice iterate through a map and generate a slice of Pair[K,V]

type SyncMap

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

func NewSyncMap

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

func NewSyncMapLength

func NewSyncMapLength[K comparable, V any](len int) *SyncMap[K, V]

func (*SyncMap[K, V]) Clear

func (me *SyncMap[K, V]) Clear()

func (*SyncMap[K, V]) Delete

func (me *SyncMap[K, V]) Delete(key K)

func (*SyncMap[K, V]) DeleteIf

func (me *SyncMap[K, V]) DeleteIf(pred func(k K, v V) bool)

func (*SyncMap[K, V]) Len

func (me *SyncMap[K, V]) Len() int

func (*SyncMap[K, V]) Load

func (me *SyncMap[K, V]) Load(key K) (value V, ok bool)

func (*SyncMap[K, V]) LoadAndDelete

func (me *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*SyncMap[K, V]) LoadAndUpdate

func (me *SyncMap[K, V]) LoadAndUpdate(key K, updateFunc func(old V) (new V, updated bool))

atomic load-and-update

func (*SyncMap[K, V]) LoadOrNew

func (me *SyncMap[K, V]) LoadOrNew(key K, newFunc func() V) (actual V, loaded bool)

atomic load-or-new

func (*SyncMap[K, V]) LoadOrNewE

func (me *SyncMap[K, V]) LoadOrNewE(key K, newFunc func() (V, error)) (actual V, loaded bool, err error)

atomic load-or-new with error check

func (*SyncMap[K, V]) LoadOrStore

func (me *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*SyncMap[K, V]) MarshalJSON

func (me *SyncMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*SyncMap[K, V]) PopAll

func (me *SyncMap[K, V]) PopAll() map[K]V

func (*SyncMap[K, V]) Range

func (me *SyncMap[K, V]) Range(f func(K, V) bool)

deprecated f must not call any methods of me. If you want to delete items using RangeNonReentrant, use DeleteIf instead.

func (*SyncMap[K, V]) RangeE

func (me *SyncMap[K, V]) RangeE(f func(K, V) error) error

f must not call any methods of me. If you want to delete items using RangeE, use DeleteIf instead.

func (*SyncMap[K, V]) RangeNonReentrant

func (me *SyncMap[K, V]) RangeNonReentrant(f func(K, V) bool)

f must not call any methods of me. If you want to delete items using Range, use DeleteIf instead.

func (*SyncMap[K, V]) Replace

func (me *SyncMap[K, V]) Replace(m map[K]V) map[K]V

Replace the map with a new one, and returns the old one

func (*SyncMap[K, V]) Store

func (me *SyncMap[K, V]) Store(key K, value V)

func (*SyncMap[K, V]) ToSlice

func (me *SyncMap[K, V]) ToSlice() []Pair[K, V]

func (*SyncMap[K, V]) UnmarshalJSON

func (me *SyncMap[K, V]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*SyncMap[K, V]) UpdateIf

func (me *SyncMap[K, V]) UpdateIf(pred func(k K, v V) (V, bool))

Jump to

Keyboard shortcuts

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