Go language Trial--mysql operation

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

Zhou Haihan/Wen 2013.8.30

Installation test

Official website http://golang.org/Download

Https://code.google.com/p/go/downloads/list

wget https://go.googlecode.com/files/go1.1.2.linux-amd64.tar.gz will generate go directory after decompression  [Andy@s1 test]$ Cat Hello.go
Package Mainimport "FMT"

Func Main () {
Fmt. Println ("Hello, World")
}

[Andy@s1 test]$ Go Build hello.go
Hello.go:3:8:cannot Find Package "FMT" in any of:
/usr/local/go/src/pkg/fmt (from $GOROOT)
($GOPATH not set)
Package Runtime:cannot Find Package "runtime" in any of:
/usr/local/go/src/pkg/runtime (from $GOROOT)
($GOPATH not set) match the environment variable: [andy@s1 ~]$ cat. Bashrcexport Goroot=/home/andy/go
Export gopath=/home/andy/go/src/pkg
Export path= $GOROOT/bin: $PATH [andy@s1 test]$ Go Build hello.go

[Andy@s1 test]$./hello
Hello, World

Gopath

Gopath is the address that git downloads to when it gets an update.

here are the tests for foreign friends on Gopath:  

rday@rday-laptop:~/golang$ mkdir packages1rday@rday-laptop:~/golang$ Export gopath=~/golang/packages1/ rday@rday-laptop:~/golang$ go get github.com/rday/webrday@rday-laptop:~/golang$ ls packages1/src/github.com/ rdayrday@rday-laptop:~/golang$ mkdir packages2rday@rday-laptop:~/golang$ Export gopath=~/golang/packages2/ rday@rday-laptop:~/golang$ go get github.com/alphazero/go-redisrday@rday-laptop:~/golang$ ls packages2/src/ github.com/alphazerorday@rday-laptop:~/golang$
When we are $GOPATH, and grab a new package, our new package is stored in the new $GOPATH directory
[Andy@s1 test]$!go
Go Build Hello.go

Test MySQL

To install the MySQL driver for go:

[Andy@s1 pkg]$ mkdir MySQL
[Andy@s1 pkg]$ cd mysql[andy@s1 mysql]$ pwd/home/andy/go/src/pkg/mysql
#[andy@s1 mysql]$ Export Gopath=/home/andy/go/src/pkg/mysql
[Andy@s1 ~]$ echo $GOPATH/home/andy/go[andy@s1 ~]$ go get github.com/go-sql-driver/mysqlwarning:gopath set to GOROOT (/ho ME/ANDY/GO) has no effectpackage github.com/go-sql-driver/mysql:cannot download, $GOPATH must is set to $GOROOT. For more details See:go help Gopath
[Andy@s1 ~]$ Echo $GOPATH/home/andy/go/src/pkg
[Andy@s1 ~]$ Go get github.com/go-sql-driver/mysql
[Andy@s1 pkg]$ Find. -name Mysql./src/github.com/go-sql-driver/mysql
[Andy@s1 pkg]$ cp-r./src/github.com/go-sql-driver/mysql MySQL
[Andy@s1 mysql]$ ls
Buffer.go      const.go   driver_test.go  infile.go  packets.go  result.go  statement.go    Utils.goconnection.go  driver.go  errors.go       LICENSE    readme.md   rows.go    transaction.go  utils_test.go[andy@s1 mysql]$ Pwd/home/andy/go/src/pkg/mysql/src/github.com/go-sql-driver/mysql
[Root@s1 mysql]# yum install mysql-devel mysql-server
[root@s1 mysql]# service MySQL restart
mysql> use test;database changedmysql> show tables; Empty Set (0.00 sec) CREATE TABLE ' student ' (  ' id ' int (one) not NULL auto_increment,  ' name ' varchar () DEFAULT NU LL,  ' age ' int (one) default NULL,  PRIMARY KEY (' id ')) Engine=myisam default charset=mysql> CREATE TABLE Studen T (ID int PRIMARY key auto_increment,name varchar (), age int,created date) DEFAULT Charset=utf8;
Query OK, 0 rows affected (0.08 sec)

[andy@s1 test]$ cat My.go

//andy Zhou 2013.8.27//http://abloz.compackage mainimport (_ "MySQL" "Database/sql" "FMT") func main () { DB: = Opendb ("Root:@/test?charset=utf8") id:=insert (DB) query (db) Update (DB,ID)}//Open database connection func opendb (Dbstr string ) (* SQL. db) {//dsn: [username[:p assword]@][protocol[(address)]]/dbname[?param1=value1&paramn=valuen] db, err: = SQL. Open ("MySQL", dbstr) Prerr (ERR) return db}//Insert data func Insert (DB * sql. db) Int64 {stmt, err: = db.    Prepare ("INSERT into student SET id=?, name=?,age=?,created=?") Prerr (Err) res, err: = stmt. Exec (0, "Abloz1", "2013-8-20") Prerr (ERR) ID, err: = Res. Lastinsertid () Prerr (err) fmt. PRINTLN (ID) return id}//update data func update (DB *sql. Db,id Int64) {stmt, err: = db. Prepare ("Update student set name=?")    where id=? ") Prerr (Err) res, err: = stmt. Exec ("ABLOZ2", id) prerr (ERR) affect, err: = Res. Rowsaffected () Prerr (err) fmt. PRINTLN (Affect)} 
Query data func query (db  * sql). db) {    rows, err: = db. Query ("SELECT * from Student")    Prerr (err) for    rows. Next () {        var id int        var name string        var department string        var created string        err = rows. Scan (&id, &name, &department, &created)        prerr (err)        FMT. Println (ID)        FMT. PRINTLN (name)        FMT. PRINTLN (department)        FMT. Println (created)    }}//Delete data func del (DB  * SQL. DB, id Int64) {    stmt, err: = db. Prepare ("Delete from student where id=?")    Prerr (Err)    res, err: = stmt. Exec (ID)    prerr (err)    affect, err: = Res. rowsaffected ()    prerr (err)    FMT. PRINTLN (Affect)}func Prerr (err error) {    If err! = Nil {        panic (err)    }} to execute: [andy@s1 test]$ Go Build my.go

[Andy@s1 test]$./my
4
1
Helloå ' ¨
30
2013-08-27
2
Abloz2
28
2013-08-20
3
Abloz2
28
2013-08-20
4
Abloz1
28
2013-08-20
1

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.