docs(context): clarify response restrictions on copied contexts

Signed-off-by: Виктор Быстров <anatema@bk.ru>
This commit is contained in:
Виктор Быстров 2026-11-12 10:09:19 +05:00
parent 0f09c3a9b4
commit 39e1cae407
2 changed files with 6 additions and 1 deletions

View File

@ -117,8 +117,11 @@ func (c *Context) reset() {
*c.skippedNodes = (*c.skippedNodes)[:0]
}
// Copy returns a copy of the current context that can be safely used outside the request's scope.
// Copy returns a copy of the current context for use outside the request's scope.
// This has to be used when the context has to be passed to a goroutine.
//
// The copy's underlying http.ResponseWriter is nil, so it cannot write the HTTP response.
// Do not call response-writing methods such as JSON, String, or Stream on the copy.
func (c *Context) Copy() *Context {
cp := Context{
writermem: c.writermem,

View File

@ -617,6 +617,8 @@ func main() {
When starting new Goroutines inside a middleware or handler, you **SHOULD NOT** use the original context inside it, you have to use a read-only copy.
The copy's underlying `http.ResponseWriter` is `nil`, so it cannot write the HTTP response. Do not call response-writing methods such as `JSON`, `String`, or `Stream` on the copy. Write the response from the original handler before it returns.
```go
func main() {
r := gin.Default()