Context and defer

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
 defer cancel()
 _, err := m.DB.Exec(ctx, stmt, email, username, password)

If the containing function gets cancelled by the context defer cancel() , what get return to the caller?

defer func(ctx context.CancelFunc) error {
err := errors.New("timeout")
ctx()
}(cancel

or is this my only option?

It’s up to the function to decide what to return. Usually it’s context.Canceled or something that wraps context.Canceled.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.