From 75b2beb8482abd56bd46ed52ab8320617adcf57f Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 27 Mar 2024 19:53:02 +0100 Subject: [PATCH] add readme --- README.md | 30 ++++++++++++++++++++++++++++++ contrib/example.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..4fd1d81 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# foreshadow + +`foreshadow` is a Go linter which detects un-shadowed contexts in loops. +They can lead to performance issues, as documented here: https://gabnotes.org/fat-contexts/ + +## Example + +```go +package main + +import "context" + +func ok() { + ctx := context.Background() + + for i := 0; i < 10; i++ { + ctx := context.WithValue(ctx, "key", i) + _ = ctx + } +} + +func notOk() { + ctx := context.Background() + + for i := 0; i < 10; i++ { + ctx = context.WithValue(ctx, "key", i) // "context not shadowed in loop" + _ = ctx + } +} +``` diff --git a/contrib/example.go b/contrib/example.go index bdbeed1..f8d416b 100644 --- a/contrib/example.go +++ b/contrib/example.go @@ -15,7 +15,7 @@ func notOk() { ctx := context.Background() for i := 0; i < 10; i++ { - ctx = context.WithValue(ctx, "key", i) + ctx = context.WithValue(ctx, "key", i) // "context not shadowed in loop" _ = ctx } }