golang-examples/example-pprof-simple/foo.go

39 lines
697 B
Go

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)
}