Skip to Content
Learning Functional Programming in Go
book

Learning Functional Programming in Go

by Lex Sheehan
November 2017
Intermediate to advanced
670 pages
17h 35m
English
Packt Publishing
Content preview from Learning Functional Programming in Go

A more practical Context use case

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781787281394Supplemental Content