Go Database/sql Documentation

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.