三生石@ 2012-07-05 23:00 采纳率: 100%
浏览 150
已采纳

从 Go 连接到 MySQL 的推荐方式是什么?

I am looking for a reliable solution to connect to a MySQL database from Go. I've seen some libraries around but it is difficult to determine the different states of completeness and current maintenance. I don't have complicated needs, but I'd like to know what people are relying on, or what's the most standard solution to connect to MySQL.

转载于:https://stackoverflow.com/questions/11353679/whats-the-recommended-way-to-connect-to-mysql-from-go

  • 写回答

2条回答 默认 最新

  • YaoRaoLov 2012-07-06 06:41
    关注

    A few drivers are available but you should only consider those that implement the database/sql API as

    • it provides a clean and efficient syntax,
    • it ensures you can later change the driver without changing your code, apart the import and connection.

    Two fast and reliable drivers are available for MySQL :

    I've used both of them in production, programs are running for months with connection numbers in the millions without failure.

    Other SQL database drivers are listed on go-wiki.

    Import when using MyMySQL :

    import (
        "database/sql"
        _ "github.com/ziutek/mymysql/godrv"
    )
    

    Import when using Go-MySQL-Driver :

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

    Connecting and closing using MyMySQL :

    con, err := sql.Open("mymysql", database+"/"+user+"/"+password)
    defer con.Close()
    // here you can use the connection, it will be closed when function returns
    

    Connecting and closing using Go-MySQL-Driver :

    con, err := sql.Open("mysql", store.user+":"+store.password+"@/"+store.database)
    defer con.Close()
    

    Select one row :

    row := con.QueryRow("select mdpr, x, y, z from sometable where id=?", id)
    cb := new(SomeThing)
    err := row.Scan(&cb.Mdpr, &cb.X, &cb.Y, &cb.Z)
    

    Select multiple rows and build an array with results :

    rows, err := con.Query("select a, b from item where p1=? and p2=?", p1, p2)
    if err != nil { /* error handling */}
    items := make([]*SomeStruct, 0, 10)
    var ida, idb uint
    for rows.Next() {
        err = rows.Scan(&ida, &idb)
        if err != nil { /* error handling */}
        items = append(items, &SomeStruct{ida, idb})
    }
    

    Insert :

    _, err = con.Exec("insert into tbl (id, mdpr, isok) values (?, ?, 1)", id, mdpr)
    

    You'll see that working in Go with MySQL is a delightful experience : I never had a problem, my servers run for months without errors or leaks. The fact that most functions simply take a variable number of arguments lighten a task which is tedious in many languages.

    Note that if, in the future, you need to use another MySQL driver, you'll just have to change two lines in one go file : the line doing the import and the line opening the connection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集