2024-03-27 19:24:38 +01:00
|
|
|
package src
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
func example() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
ctx := context.WithValue(ctx, "key", i)
|
|
|
|
ctx = context.WithValue(ctx, "other", "val")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2024-03-27 23:50:12 +01:00
|
|
|
ctx = context.WithValue(ctx, "key", i) // want "nested context in loop"
|
2024-03-27 19:24:38 +01:00
|
|
|
ctx = context.WithValue(ctx, "other", "val")
|
|
|
|
}
|
|
|
|
|
|
|
|
for item := range []string{"one", "two", "three"} {
|
2024-03-27 23:50:12 +01:00
|
|
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
2024-03-27 19:24:38 +01:00
|
|
|
ctx := context.WithValue(ctx, "key", item)
|
|
|
|
ctx = wrapContext(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2024-03-27 23:50:12 +01:00
|
|
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
2024-03-27 19:24:38 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapContext(ctx context.Context) context.Context {
|
|
|
|
return context.WithoutCancel(ctx)
|
|
|
|
}
|