lck

package module
v0.0.0-...-b06db16 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: MIT Imports: 3 Imported by: 3

README

go-lck

Package lck implements thread-safe locking-types, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-lck

GoDoc

Examples

Here is an example locking-type that can hold a bool:

import "github.com/reiver/go-lck"

//

var lockable lck.Locking[bool]

// ...

lockable.Set(true)

// ...

value := lockable.Get()

Here is another example locking-type, where here it holds a map:

import "github.com/reiver/go-lck"

//

var lockable lck.Locking[map[string]any]

// ...

lockable.Let(fn(m *map[string]any) {
	if nil == *m {
		*m = map[string]any{}
	}

	(*m)["something"] = 5
})

// ...

var value any

lockable.Let(fn(m *map[string]any) {
	if nil == *m {
		return
	}

	value = *m["something"]
})

Import

To import package lck use import code like the follownig:

import "github.com/reiver/go-lck"

Installation

To install package lck do the following:

GOPROXY=direct go get github.com/reiver/go-lck

Author

Package lck was written by Charles Iliya Krempeaux

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lockable

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

func (*Lockable[T]) Get

func (receiver *Lockable[T]) Get() T

func (*Lockable[T]) Let

func (receiver *Lockable[T]) Let(fn func(*T))

func (*Lockable[T]) Set

func (receiver *Lockable[T]) Set(value T)

type Map

type Map[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}
Example (String_int)
var store lck.Map[string, int]

fmt.Printf("INITIAL-STORE-LEN: %d\n", store.Len())

store.Set("once", 1)
store.Set("twice", 2)
store.Set("thrice", 3)
store.Set("fource", 4)

fmt.Printf("STORE-LEN-AFTER-SETTING: %d\n", store.Len())

value, found := store.Get("twice")
fmt.Printf("STORE[twice]: %d, %t\n", value, found)

fmt.Println()
fmt.Println("KEY-VALUES:")
store.For(func(key string, value int) {
	fmt.Println()
	fmt.Printf("- KEY: %q\n", key)
	fmt.Printf("- VALUE: %d\n", value)
})

store.Unset("twice")

fmt.Println()
fmt.Println("KEY-VALUES:")
store.For(func(key string, value int) {
	fmt.Println()
	fmt.Printf("- KEY: %q\n", key)
	fmt.Printf("- VALUE: %d\n", value)
})
Output:

INITIAL-STORE-LEN: 0
STORE-LEN-AFTER-SETTING: 4
STORE[twice]: 2, true

KEY-VALUES:

- KEY: "fource"
- VALUE: 4

- KEY: "once"
- VALUE: 1

- KEY: "thrice"
- VALUE: 3

- KEY: "twice"
- VALUE: 2

KEY-VALUES:

- KEY: "fource"
- VALUE: 4

- KEY: "once"
- VALUE: 1

- KEY: "thrice"
- VALUE: 3

func (*Map[K, V]) For

func (receiver *Map[K, V]) For(fn func(K, V))

func (*Map[K, V]) Get

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

func (*Map[K, V]) Len

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

func (*Map[K, V]) Set

func (receiver *Map[K, V]) Set(key K, value V)

func (*Map[K, V]) Unset

func (receiver *Map[K, V]) Unset(key K)

Jump to

Keyboard shortcuts

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