Missing Cancellation Token (ARCH5024)
هذا المحتوى غير متوفر بلغتك بعد.
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.
Incorrect
Section titled “Incorrect”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)}Correct
Section titled “Correct”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.