Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx
"
In this case, what are the drawbacks of storing transaction in the context?
I'd like input on whether the following idea is good or bad, and why.Consider an abstract transaction, modeled as follows:func Transaction(ctx context.Context, body func(ctx context.Context) error) error
func OnCommit(ctx context.Context, commitHook func(ctx context.Context))func OnRollback(ctx context.Context, rollbackHook func(ctx context.Context))The code that wishes to execute a transaction would call Transaction and provide the body of the transaction as a function parameter. If that function returns an error, the transaction is rolled back, by calling any hooks registered during that transaction by calling OnRollback. If the body function returns success, the transaction is committed by calling any hooks registered during that transaction by calling OnCommit.The implication of this model is that the identity of what transaction you're in is stored in the context. The ctx passed into body has that value added to it, and the OnCommit and OnRollback calls associate the hooks with the transaction they find in the context.Of course the same thing could be done by passing the transaction explicitly, e.g.func Transaction(ctx context.Context, body func(ctx context.Context, tx Tx) error) error
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.