trying in vain to figure out which goroutine I'm in
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
95c4fa7437
commit
33f46be98b
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue