threadlocal

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup()

Cleanup deletes the local storage for the current go routine

func Delete

func Delete(key string)

Delete deletes a variable from the local storage of the current go routine

func Get

func Get(key string) (interface{}, bool)

Get returns a variable from the local storage of the current go routine

func Go

func Go(f func())

Go executes the given function in a go routine and ensures that the local storage is initialized before the function is called and deleted before after the function returns or panics

Example
Init()
var wg sync.WaitGroup
wg.Add(2)

Set(`key`, `main value`)
Go(func() {
	Set(`key`, `value 1`)
	Go(func() {
		Set(`key`, `value 2`)
		v, _ := Get(`key`)
		fmt.Printf("g2 = %s\n", v)
		Delete(`key`)
		wg.Done()
	})
	v, _ := Get(`key`)
	fmt.Printf("g1 = %s\n", v)
	Delete(`key`)
	wg.Done()
})
wg.Wait()
v, _ := Get(`key`)
fmt.Printf("main = %v\n", v)
Output:

g1 = value 1
g2 = value 2
main = main value

func Init

func Init()

Init initializes a go routine local storage for the current go routine

func Set

func Set(key string, value interface{})

Set adds or replaces a variable to the local storage of the current go routine

Types

This section is empty.

Jump to

Keyboard shortcuts

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