This is a creation in Article, where the information may have evolved or changed.
18. Toad Notes Go language--access to the database
Databases are at the heart of many Web applications.
A database can be used to store almost any information you want to query and modify, such as user information, product catalogs, or news lists.
Go does not have a built-in driver to support any database, but go defines the Database/sql interface, which allows the user to develop the corresponding database driver based on the driver interface.
Today, NoSQL has become a trend in web development, and many applications use NoSQL as a database rather than a previous cache.
Database/sql interface
Go differs from PHP in that go does not have an official database driver, but rather a standard interface for developers to develop database drivers that can be developed according to defined interfaces, so as long as the code developed according to the standard interface is needed to migrate the database later, No modifications are required.
Sql. Register
Functions that exist in Database/sql are used to register the database driver, and when a third-party developer develops a database driver, it implements the Init function, which in INIT calls the register (name string, driver driver. Driver) to complete the registration of this driver.
Third-party database drivers are called by this function to register their own database driver name and the corresponding driver implementation. A map is stored inside the Database/sql to store the user-defined corresponding driver.
Multiple database drivers can be registered at the same time through the Database/sql registration function, as long as they are not duplicated.
Driver. Driver
Driver is a database-driven interface that defines a method:open (namestring) that returns the Conn interface of a database.
The returned conn can only be used for one goroutine operation, which means that the conn cannot be applied to multiple goroutine of go.
Driver. Conn
Conn is an interface definition for a database connection, and he defines a series of methods that can only be used within a goroutine and not in multiple goroutine.
Driver. Stmt
stmt is a ready state, associated with Conn, and can only be applied to one goroutine, and cannot be applied to more than one goroutine.
Driver. Tx
Transaction processing generally takes two processes, either submitting or rolling back. The database driver can only implement these two functions.
Driver. Execer
is an interface that conn can choose to implement
Driver. Result
Is the result interface definition that is returned by performing operations such as Update/insert
Driver. Rows
Rows is the result set interface definition that executes the query return
Driver. rowsaffected
is a int64 alias, but he implements the result interface for the underlying implementation of the result representation
Driver. Value
Value is actually an empty interface, he can hold any data
Driver. Valueconverter
The Valueconverter interface defines how to convert an ordinary value into a driver.value interface.
Driver. Valuer
The valuer interface defines the way to return a driver.value
A driver as long as the implementation of these interfaces will be able to complete the deletion and modification of the basic operations, and the rest is the corresponding database for data interaction and other details of the problem
Database/sql
Based on the interfaces provided by Database/sql/driver, Database/sql defines a number of higher-order methods to simplify database operations while also internally recommending a Conn pool.