/goid

Primary LanguageGoMIT LicenseMIT

GoID

GoDoc Widget Build Status codecov Go Report Card

Hacking runtime to get goroutine id for caching meta

Usage

# Patch
go get -u github.com/go-courier/goid/patch-runtime && patch-runtime

For go module user:

need run blow

cd $GOPATH/src
mkdir -p global-tools
cd global-tools
go mod init
package goid_test

import (
	"time"
	"fmt"
	"math/rand"

	"github.com/go-courier/goid"
)

func ExampleLogIDMap() {
	for i := 0; i < 100; i ++ {
		go func() {
			// set logid at begin of goroutine
			goid.Default.Set(fmt.Sprintf("%d", rand.Int()))
			// clear at end of goroutine
			defer goid.Default.Clear()

			// do something with the cached logid
			_ = goid.Default.Get()

			time.Sleep(10 * time.Millisecond)
		}()
	}

	time.Sleep(5 * time.Millisecond)
	fmt.Println(len(goid.Default.All()))

	time.Sleep(50 * time.Millisecond)
	fmt.Println(len(goid.Default.All()))
	// Output:
	//100
	//0
}