This is a created article in which the information may have evolved or changed.
Overview of the document
The usual way to use SQL or SQL-like databases in Golang is through the Database/sql package operation. It provides a lightweight interface for row-oriented databases. This article is about how to use it, the most common reference.
Why do you need this? Pack documentation file tells you what everything has done, but it doesn't tell you how to use this package. Many of us want to be quick to refer to and get started instead of telling stories. Welcome to donate; Please send your request here.
In Golang you use SQL.DB to access the database. You can use this type to create statements and transactions, execute queries, and get results. The following code lists the SQL. DB is a struct, click Database/sql/sql.go to view the official source code.
First you should know a SQL. DB is not a connection to a database. It also does not map to any of the features of the database software "database" or "mode" concept. It is an abstraction of the database's interface and database, which may be different from local files, accessible over a network connection, or accessed in memory and process.
Sql. DB performs some important tasks behind the scenes for you:
• Open and close the actual underlying database connection through the driver.
• It manages a connection pool as needed, which may be a variety of things as described above.
Sql. The DB abstraction is designed so you don't have to worry about managing concurrent access to the underlying data store. A connection is marked as available when it is used to perform a task, and then returned to the available pool when it is not in use. One consequence of this is that if you cannot release the connection to the pool, it can result in a db. SQL opens a large number of connections and may run out of resources (too many connections, too many open file handles, missing available network ports, and so on). We will discuss the issue further later.
When you create SQL. DB, you can use it to query the database it represents, as well as create statements and transactions.
No.2 Importing Database Drivers
To use Database/sql, you need to database/sql yourself and the specific database drivers that you need to use.