November 2017
Intermediate to advanced
670 pages
17h 35m
English
A more practical Context use case would be to send a Cancel message to a long-running function.
Several use cases come to mind when dealing with database transactions.
In some cases a request could generate a number of child requests, each running for varying amounts of time and consuming various resources. If during our database transaction, one of our child requests panics, we could use the Context to signal all routines to cancel and to free up all transaction-related resources:
import ( "database/sql" "github.com/pkg/errors")
Provide access to the sql.DB commit and rollback:
type Transaction interface { Commit() error Rollback() error}
The TxFunc param is a functional parameter provided to the db.WithTransaction ...