weakmap

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 3 Imported by: 0

README

weakmap

WeakMap for Go without AddCleanup or SetFinalizer

Go Reference

Documentation

Overview

Package weakmap implements weak map, a map which references its values without preventing reclamation of memory used by them.

Index

Constants

View Source
const DefaultN = 5

DefaultN is overhead goal parameter, which corresponds to 25% inactive elements overhead.

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 implements weak map. It is safe for concurrent use by multiple goroutines.

func Make

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

Make creates new weak map with default overhead goal.

func Make1

func Make1[K comparable, V any](n int) *Map[K, V]

Make1 creates new weak map with specified overhead goal parameter.

func (*Map[K, V]) Delete

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

Delete removes an element from map.

func (*Map[K, V]) Flush

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

Flush empties map

func (*Map[K, V]) Get

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

Get lookups key in weak map. It returns nil pointer if key doesn't exist in map or its value was already reclaimed.

func (*Map[K, V]) Len

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

Len returns number of weak pointers (active or not) currently in map.

func (*Map[K, V]) Range added in v0.1.2

func (m *Map[K, V]) Range(f func(key K, value *V) bool)

Range iterates over all map elements.

func (*Map[K, V]) Set

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

Set adds new item to weak map or updates existing one.

Jump to

Keyboard shortcuts

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