Go language connection MySQL database

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Go language connection MySQL database


PHP is fast to develop, but performance needs to be improved.
C and C + +? Not good at reading other people's code is difficult, not to mention their own writing.
Introduced by people, began to use the go language, development speed is relatively fast, performance is also good.


http://golang.org/


Recently need to connect MySQL database using go language,
The first reaction is to find the official package:


http://golang.org/pkg/database/


There are two package that don't seem to be a reliable one:


Sql:
Package SQL provides a generic interface around SQL (or sql-like) databases.
Driver
Package driver defines interfaces to be implemented by database drivers as used by package SQL.
Then go to the Go Project Dashboard and look for it, http://godashboard.appspot.com/.
There are several MySQL-related:


Gomysql-mysql Library for Go Lib
Gomysql-client-library-go MySQL Client Library Lib
Mymysql-mysql Client API written entirely in Go. Lib
GO-DBD-MYSQL-A MySQL driver implementation for go-dbi Lib
Go-dbi-a Database Abstraction API in the spirit of Perl DBI et al Lib
Go-mysql-mysql wrapper for Go CGO
Go-mysql-driver-a lightweight and fast mysql-driver for Go ' s Database/sql package lib
Libmysqlgo-another wrapper for the MySQL C API CGO
Mysql-connector-go-implements MySQL Wire Protocol lib
Mysqlgo-mysql Bindings CGO
Gomysql go-dbd-mysql go-dbi Go-mysql can't be installed ... Do not know is the environmental problem or what problem, did not look closely.
Gomysql-client-library and Libmysqlgo link to the 404 error page.
Mysql-connector-go link to 403 error page ...
Mysqlgo has defunct.
Mymysql and Go-mysql-driver installed successfully.


First Look at Go-mysql-driver:


Package Main

Import (
"FMT"
)


Import "Database/sql"
Import _ "Code.google.com/p/go-mysql-driver/mysql"




Func Main () {
DB, _: = SQL. Open ("MySQL", "test:123456@tcp (localhost:3306)/shipincon?charset=utf8")

Row: = db. Queryrow ("Select nickname from User Limit 1")
var nickname string
Row. Scan (&nickname)
Fmt. Println (nickname)
Db. Exec ("TRUNCATE table T");

Db. Exec ("INSERT INTO t (SELECT * from User order by user_id ASC limit 5)")

Rows, _: = db. Query ("Select Nickname, user_id, type from T limit 2");
For rows. Next () {
var nickname string
var t string
var user_id string

Rows. Scan (&nickname, &user_id, &t)
Fmt. Println (nickname, user_id, T)
}

}


[Root@www go]# Go Run test.go
Zhao Tianhua
Zhao Tian 1 Renren
Mould 2 Renren
Looks good, very useful.
Disadvantages?
Rows. Scan, each col in the table needs a variable corresponding to it, the field is more than tired?


Mymysql's performance in this respect is better:


Package Main

Import (
"FMT"
"OS"
"Github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/native"//Native engine
_ "Github.com/ziutek/mymysql/thrsafe"//Thread Safe engine
)

Func Main () {
DB: = MySQL. New ("TCP", "", "127.0.0.1:3306", "Test", "123456", "Shipincon")

ERR: = db. Connect ()
If err! = Nil {
Panic (ERR)
}

Db. Query ("Set names UTF8")

Rows, res, err: = db. Query ("Select nickname,type,user_id from user where user_id >%d limit 2", 20)
If err! = Nil {
Panic (ERR)
}

For _, Row: = Range Rows {
First: = Res. Map ("user_id")
Second: = Res. Map ("type")
Third: = Res. Map ("nickname")
Val1, Val2, Val3: = row. Int (first), row. STR (second), row. STR (third)
Fmt. Println (Val1, VAL2,VAL3)
}
}

[Root@www go]# Go Run t.go
Renren Zhao Tianxiang
Renren Liu Jing
It looks mymysql more mature.


Official documents:


Http://go.pkgdoc.org/github.com/ziutek/mymysql
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.