This is a creation in Article, where the information may have evolved or changed.
1. Download Go-sql-driver/mysql in Golib
Go get Github.com/go-sql-driver/mysql
2. Code Introduction
Import (
"Database/sql"
"Github.com/go-sql-driver/mysql"
)
3. Create a DB?
DB, err: = SQL. Open ("MySQL", "User:password@/dbname")
NOTE: SQL. DB is an encapsulated driver package that provides advanced APIs that provide secure, multi-connected operations.
4. Documentation:
Http://godoc.org/github.com/go-sql-driver/mysql
http://godoc.org/
http://localhost:8080/pkg/database/sql/#DB
5. Summary:
The main interface of Database/sql
Sql. register{
UNC Register (name string, driver driver. Driver) Error
}
The main interface of Database/sql/driver
Type Driver Interface {
Open (name string) (Conn, error)
}
Type Conn Interface {Prepare (query string) (Stmt, error) Close () Error Begin () (Tx, error)}
Type Stmt Interface {Close () error numinput () int Exec (args []value) (Result, error) Query (args []value] (Rows, error)}
Type Tx Interface {Commit () error Rollback () Error}type Result interface {Lastinsertid () (Int64, error) rowsaffected () ( INT64,ERROR)}type Rows Interface {Columns () []string Close () error Next (dest []value) error}type execer type Value type V Alueconverter type valuer
But Database/sql defines a higher-level api:sql. DB, which is multi-concurrency reusable. Type db func Open (drivername, datasourcename string) (*db, error) func (db *db) Begin () (*TX, error) func (db *DB) Close () error func (DB *db) Driver () Driver. Driver func (db *db) Exec (query string, args ... interface{}) (Result, error) func (db *db) Ping () error func (db *db) Prep Is (query string) (*stmt, error) func (db *db) query (query string, args ... interface{}) (*rows, error) func (db *db) query Row (query string, args ... interface{}) *row func (db *db) Setmaxidleconns (n int) func (db *db) Setmaxopenconns (n int)