duanlinzhen7235 2012-07-05 23:00
浏览 28
已采纳

从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.

  • 写回答

2条回答 默认 最新

  • doutuan4361 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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改