clonectx

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2019 License: MIT Imports: 1 Imported by: 0

README

clonectx

GoDoc GitHub release GitHub

baseCtx, cancel := context.WithCancel(context.Background())
baseCtx = context.WithValue(baseCtx, "user_id", 123)

newCtx := clonectx.Clone(baseCtx)
cancel()

fmt.Println(newCtx.Value("user_id"), baseCtx.Value("user_id")) // => 123 123
fmt.Println(newCtx.Err(), baseCtx.Err()) // => <nil> context canceled

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone(base context.Context) context.Context

Clone creates a new Context object but inherits values from the base context object.

Example
package main

import (
	"context"
	"fmt"

	"github.com/izumin5210/clonectx"
)

func main() {
	debug := func(ctx context.Context) {
		fmt.Println(
			"user_id: ", ctx.Value("user_id"), "\t",
			"request_id: ", ctx.Value("request_id"), "\t",
			"error: ", ctx.Err(),
		)
	}

	baseCtx, cancel := context.WithCancel(context.Background())
	baseCtx = context.WithValue(baseCtx, "user_id", 123)
	baseCtx = context.WithValue(baseCtx, "request_id", "7de4f345-8a68-47bc-a1df-90299f95d753")

	newCtx := clonectx.Clone(baseCtx)
	cancel()

	debug(baseCtx)
	debug(newCtx)
}
Output:

user_id:  123 	 request_id:  7de4f345-8a68-47bc-a1df-90299f95d753 	 error:  context canceled
user_id:  123 	 request_id:  7de4f345-8a68-47bc-a1df-90299f95d753 	 error:  <nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

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