xcontext

package
v0.0.57-rc.4 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package xcontext is a package to offer the extra functionality we need from contexts that is not available from the standard context package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Detach

func Detach(ctx context.Context) context.Context

Detach returns a context that keeps all the values of its parent context but detaches from the cancellation and error handling.

Example
package main

import (
	"context"

	"github.com/komuw/ong/xcontext"
)

type ctxKey string

func log(ctx context.Context, logMsg string)   {}
func sendMail(ctx context.Context, msg string) {}

func main() {
	key := ctxKey("key")
	ctx := context.WithValue(context.Background(), key, "my_value")

	// Detach is not required here.
	log(ctx, "api called.")

	// We need to use Detach here, because sendMail(having been called in a goroutine) can outlive the cancellation of the parent context.
	go sendMail(xcontext.Detach(ctx), "hello")

}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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