Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvia7788 committed Aug 16, 2022
1 parent 6227d90 commit c334f8a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func call1(ctx context.Context) {
call2(context.Background()) // Non-inherited new context, use function like `context.WithXXX` instead

call3() // Function `call3` should pass the context parameter
call4() // Function `call4->call3` should pass the context parameter
...
}

Expand All @@ -29,10 +30,68 @@ func call3() {
call2(ctx)
}

func call4() {
call3()
}


// if you want none-inherit ctx, use this function
func getNewCtx(ctx context.Context) (newCtx context.Context) {
...
return
}

/* ---------- check net/http.HandleFunc ---------- */

func call5(ctx context.Context, w http.ResponseWriter, r *http.Request) {
}

func call6(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
call5(ctx, w, r)
call5(context.Background(), w, r) // Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead
}

func call7(in bool, w http.ResponseWriter, r *http.Request) {
call5(r.Context(), w, r)
call5(context.Background(), w, r)
}

func call8() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
call5(r.Context(), w, r)
call5(context.Background(), w, r) // Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead

call6(w, r)

// call7 should be like `func call7(ctx context.Context, in bool, w http.ResponseWriter, r *http.Request)`
call7(true, w, r) // Function `call7` should pass the context parameter
})
}
```

## Tips

You can break ctx inheritance by this way, eg: [issue](https://github.com/sylvia7788/contextcheck/issues/2).

```go
func call1(ctx context.Context) {
...

newCtx, cancel := NoInheritCancel(ctx)
defer cancel()

call2(newCtx)
...
}

func call2(ctx context.Context) {
...
}

func NoInheritCancel(_ context.Context) (context.Context,context.CancelFunc) {
return context.WithCancel(context.Background())
}
```

## Installation
Expand Down

0 comments on commit c334f8a

Please sign in to comment.