trying in vain to figure out which goroutine I'm in

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-02-23 21:18:19 -06:00
parent 95c4fa7437
commit 33f46be98b
2 changed files with 42 additions and 7 deletions

View File

@ -1,9 +1,6 @@
run:
GO111MODULE="off" go run -v -x main.go
prep:
GO111MODULE="off" go get -v .
build:
GO111MODULE="off" go build -v -x
# go build
@echo
@echo "Run foo.go:"
@echo
GO111MODULE="off" go run foo.go

View File

@ -0,0 +1,38 @@
package main
import (
"runtime/pprof"
"context"
"fmt"
"time"
)
func main() {
type favContextKey string
f := func(ctx context.Context, k string) {
ctx2 := context.WithValue(context.Background(), "language", "Go2")
if v := ctx2.Value(k); v != nil {
fmt.Println("found value:", v)
} else {
fmt.Println("key not found:", k)
}
pprof.SetGoroutineLabels(ctx)
if v := ctx.Value(k); v != nil {
fmt.Println("found value:", v)
} else {
fmt.Println("key not found:", k)
}
}
ctx := context.WithValue(context.Background(), "language", "Go")
f(ctx, "language")
f(ctx, "color")
go f(ctx, "language")
go f(ctx, "color")
time.Sleep(time.Duration(1) * time.Second)
}