Opening a database handle
The database/sql
package simplifies
database access by reducing the need
for you to manage connections. Unlike many data access APIs, with
database/sql
you don’t explicitly open a connection, do work, then close
the connection. Instead, your code opens a database handle that represents
a connection pool, then executes data access operations with the handle,
calling a Close
method only when needed to free resources, such as those
held by retrieved rows or a prepared statement.
In other words, it’s the database handle, represented by an
sql.DB
, that
handles connections, opening and closing them on your code’s behalf. As your
code uses the handle to execute database operations, those operations have
concurrent access to the database. For more, see