这里用比较流行的MySQL驱动Go-SQL-Driver/MySQL,这里不用驱动提供的方法,所以进行匿名引用。
在MySQL数据库下建一个表

CREATE TABLE IF NOT EXISTS `userinfo` (
  `uid` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(64) DEFAULT NULL,
  `departname` varchar(64) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;

编译以下代码

package main

import (
    "database/sql"
    "fmt"
    "time"

    _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, err := sql.Open("mysql", "mytest:3YAhHxpRRL@tcp(192.168.2.2:3306)/mytest?charset=utf8")
    checkErr(err)

    //插入数据
    stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
    checkErr(err)

    timenow := time.Now()

    res, err := stmt.Exec("张三", "研发部门1", timenow)
    checkErr(err)

    id, err := res.LastInsertId()
    checkErr(err)

    fmt.Printf("刚才插入的是第%d行\r\n", id)

    res, err = stmt.Exec("李四", "研发部门2", timenow)
    checkErr(err)

    id, err = res.LastInsertId()
    checkErr(err)

    fmt.Printf("刚才插入的是第%d行\r\n", id)
    //更新数据
    stmt, err = db.Prepare("update userinfo set username=? where uid=?")
    checkErr(err)

    res, err = stmt.Exec("王五", id)
    checkErr(err)

    affect, err := res.RowsAffected()
    checkErr(err)

    fmt.Printf("刚才修改了%d行\r\n", affect)

    //查询数据
    rows, err := db.Query("SELECT * FROM userinfo")
    checkErr(err)

    fmt.Println("以下是查询结果:")

    for rows.Next() {
        var uid int
        var username string
        var department string
        var created string
        err = rows.Scan(&uid, &username, &department, &created)
        checkErr(err)
        fmt.Println(uid)
        fmt.Println(username)
        fmt.Println(department)
        fmt.Println(created)
    }

    //删除数据
    stmt, err = db.Prepare("delete from userinfo where departname=?")
    checkErr(err)

    res, err = stmt.Exec("研发部门1")
    checkErr(err)

    affect, err = res.RowsAffected()
    checkErr(err)

    fmt.Printf("刚才删除了%d行\r\n", affect)

    db.Close()

}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}