gls

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

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

Go to latest
Published: Apr 7, 2015 License: MIT Imports: 6 Imported by: 33

README

gls GoDoc Build Status Coverage Status

GoRoutine local storage using GoRoutine IDs

This package is heavily inspired by jtolds' gls package.

I made my own version of jtolds' package because Brad Fitzpatrick created a function to get the current GoRoutine ID. Am I a horrible person for using this function? Probably.

Why is this useful?

So far, the only thing I'm using it for is storing a unique identifier for a given HTTP request so I can track its progress through my code via logging. I felt this approach was easier and less messy than refactoring every function to take some kind of context or identifier.

Enjoy!

Documentation

Overview

Package gls implements goroutine-local storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup()

Cleanup removes all data associated with this goroutine. If this is not called, the data may persist for the lifetime of your application. This must be called from the very first goroutine to invoke Set

func Get

func Get(key string) interface{}

Get gets the value by key as it exists for the current goroutine.

func Go

func Go(f func())

Go creates a new goroutine and runs the provided function in that new goroutine. It also associates any key,value pairs stored for the parent goroutine with the child goroutine. This function must be used if you wish to preserve the reference to any data stored in gls. This function automatically cleans up after itself. Do not call cleanup in the function passed to this function.

func Set

func Set(key string, value interface{})

Set sets the value by key and associates it with the current goroutine.

func SetValues

func SetValues(values Values)

SetValues replaces all values for this goroutine.

func With

func With(values Values, f func())

With is a convenience function that stores the given values on this goroutine, calls the provided function (which will have access to the values) and then cleans up after itself.

Types

type Values

type Values map[interface{}]interface{}

Values is simply a map of key types to value types. Used by SetValues to set multiple values at once.

Jump to

Keyboard shortcuts

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