Awesome Go

contextplus

CategoryUtilities
SubcategoryGeneral utilities and tools to make your life easier.
Stars16

Package contextplus provide more easy to use functions for contexts.

About contextplus

Go Report Card PkgGoDev Go package codecov Mentioned in Awesome Go

contextplus

Package contextplus provide more easy to use functions for contexts.

Use

go get -u github.com/contextplus/contextplus

Example

This is how an http.Handler should run a goroutine that need values from the context. Pretend to use the middleware that timeout after one second.

func myMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		ctx, cancel := context.WithTimeout(time.Second)
		defer cancel()
		r = r.WithContext(ctx)
		next.ServeHTTP(w, r)
	})
}
func handle(w http.ResponseWriter, r *http.Request) {
	asyncCtx := contextplus.WithOnlyValue(r.Context())
	go func() {
		// will not cancel if timeout
		// will not cancel if call 'cancel'
		asyncTask(asyncCtx)
	}()
}
func handle(w http.ResponseWriter, r *http.Request) {
	asyncCtx := contextplus.WithoutCancel(r.Context())
	go func() {
		// will cancel if timeout
		// will not cancel if call 'cancel'
		asyncTask(asyncCtx)
	}()
}

Frequently Asked Questions

What is contextplus?

contextplus is a Utilities library for the Go programming language. Package contextplus provide more easy to use functions for contexts.

How do I install contextplus?

Install contextplus with the Go module system using `go get contextplus/contextplus`. Check the repository for the current installation instructions.

What category does contextplus belong to?

contextplus is listed under Utilities, specifically General utilities and tools to make your life easier..

← Back to General utilities and tools to make your life easier.