Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for Using built-in types in context.WithValue

$
0
0

Here is a simple example. Both packages track whether or not they've started using the key "started". A.Start calls B.Start. B.Start panics because it thinks its already started.

package mainimport "context"func main() {  A.Start(context.Background())}package Aimport "context"func Start(ctx context.Context) {  if ctx.Value("started") == true {    panic("already started")  }  newCtx := context.WithValue(ctx, "started", true)  B.Start(newCtx)}package Bimport "context"func Start(ctx context.Context) {  if ctx.Value("started") == true {    panic("already started")  }}

Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>