simplegoroutinelocal

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2022 License: MIT Imports: 7 Imported by: 0

README

Simple-goroutine-local is a cross goroutine storage tool with very simple implementation and function (the concept is similar to Java ThreadLocal).

Getting Started

Installing

To start using GJSON, install Go and run go get:

$ go https://github.com/polarbear567/simple-goroutine-local

This will retrieve the library.

How to use

package main

import (
    "fmt"
    sgl "github.com/polarbear567/simple-goroutine-local"
    "sync"
)

func main() {
    gl := sgl.NewGoRoutineLocal()
    var wg sync.WaitGroup
    wg.Add(5)
    gl.Set("value", "main")
    for i := 0; i < 5; i++ {
        go func(i int) {
            defer func() {
                wg.Done()
                gl.Del(i)
            }()
            gl.Set("value", i)
            v, _ := gl.Get("value")
            fmt.Printf("i: %d, v: %d\n", i, v)
        }(i)
    }
    wg.Wait()
    v, _ := gl.Get("value")
    fmt.Printf("main, value: %s\n", v)
    gl.Del("value")
    v, ok := gl.Get("value")
    fmt.Printf("main, value after delete: %v, value exist: %t\n", v, ok)
    gl.DelMap()
    if v, ok = gl.Get("value"); !ok {
        fmt.Printf("main, map exist: %t\n", ok)
    }
}

The output:

i: 4, v: 4
i: 2, v: 2
i: 1, v: 1
i: 0, v: 0
i: 3, v: 3
main, value: main
main, value after delete: <nil>, value exist: false
main, map exist: false

Tip

Since goroutine may be reused, please make sure to explicitly set in the current goroutine before get, or delete the key before the end of the current goroutine.

Documentation

Overview

*

  • @author Leo Li

*

  • @author Leo Li

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurGoroutineID

func CurGoroutineID() uint64

Types

type GoRoutineLocal

type GoRoutineLocal struct {
	// contains filtered or unexported fields
}

func NewGoRoutineLocal

func NewGoRoutineLocal() *GoRoutineLocal

func (*GoRoutineLocal) Del

func (m *GoRoutineLocal) Del(k interface{})

func (*GoRoutineLocal) DelMap

func (m *GoRoutineLocal) DelMap()

func (*GoRoutineLocal) Get

func (m *GoRoutineLocal) Get(k interface{}) (interface{}, bool)

func (*GoRoutineLocal) Set

func (m *GoRoutineLocal) Set(k, v interface{})

Jump to

Keyboard shortcuts

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