Ir al contenido

Missing Cancellation Token (ARCH5024)

Esta página aún no está disponible en tu idioma.

Key go.async.missing-context-param · Severity warning · Category async

Public or long-running operations should accept a context.Context parameter and propagate it to downstream calls so the work can be cancelled. Without it, work keeps running after the caller has gone away.

func FetchUser(id string) (*User, error) {
resp, err := http.Get("https://api.example.com/users/" + id)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return decodeUser(resp.Body)
}
func FetchUser(ctx context.Context, id string) (*User, error) {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet,
"https://api.example.com/users/"+id, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return decodeUser(resp.Body)
}

Add ctx context.Context as the first parameter and propagate it to downstream calls.