How it works...

This recipe works in a very similar way to the previous database recipe Using the database/sql package with MySQL. This recipe performs the same operation of creating data and querying it, but also demonstrates using transactions and making generic database functions that work with both sql.DB connections and sql.Transaction objects. 

Code written in this way allows us to reuse function that perform database operations that can be run individually or in groups using a transaction. This allows for more code reuse while still isolating functionality to functions or methods operating on a database. For example you can have Update(db DB) functions for multiple tables and pass them all a shared transaction to perform multiple updates atomically. It's also simpler to mock these interfaces, as you'll see in Chapter 9, Testing Go Code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.140