This is a creation in Article, where the information may have evolved or changed.
All the first to update everyone to look at, and then wait for the time I give you to these code home comments, all code everyone can view go.lingphp.com I put it here.
[PHP]
Package Main
Import (
"Database/sql"
"FMT"
_ "Github.com/go-sql-driver/mysql"
)
Type User struct {
ID int
Username, password string
}
var sqldata map[interface{}]interface{}
Func Main () {
var u User
DB, err: = SQL. Open ("MySQL", "Root:@/test?charset=utf8")
Check (ERR)
Inserting data
stmt, err: = db. Prepare ("INSERT user SET username=?,password=?")
Check (ERR)
Res, err: = stmt. Exec ("Xiaowei", "Xiaowei")
Check (ERR)
ID, Err: = Res. Lastinsertid ()
Check (ERR)
Fmt. PRINTLN (ID)
Querying data
Rows, err: = db. Query ("SELECT * from User")
Check (ERR)
Fmt. Println (rows. Columns ())
UserInfo: = Make (map[interface{}]interface{})
For rows. Next () {
ERR: = rows. Scan (&u.id, &u.username, &u.password)
Check (ERR)
Userinfo[u.id] = u
}
Fmt. Println (UserInfo)
}
Func Check (err error) {
If err! = Nil {
Fmt. PRINTLN (ERR)
}
}
[/php]
Do not reprint any article of this station without permission: Micro network»golang operation MySQL Database