Home > Backend Development > Golang > What's the Best Way to Connect to MySQL from Go Using the `database/sql` API?

What's the Best Way to Connect to MySQL from Go Using the `database/sql` API?

DDD
Release: 2024-12-24 01:30:15
Original
638 people have browsed it

What's the Best Way to Connect to MySQL from Go Using the `database/sql` API?

The Best Way to Connect to MySQL from Go

Reliable MySQL Connection in Go

Connecting to MySQL from Go is made easy with the appropriate driver. Among the available options, those that adhere to the database/sql API are recommended for their:

  • Clean and efficient syntax
  • Compatibility across multiple drivers without code modification (except import and connection)

Top MySQL Drivers for Go

Two fast and reliable drivers stand out for MySQL connection:

  • MyMySQL
  • Go-MySQL-Driver

Production-tested experience confirms their stability and performance.

Import Statements for Drivers

  • MyMySQL: import (_ "github.com/ziutek/mymysql/godrv")
  • Go-MySQL-Driver: import (_ "github.com/go-sql-driver/mysql")

Connection and Closing

  • MyMySQL:

    con, err := sql.Open("mymysql", database+"/"+user+"/"+password)
    defer con.Close()
    Copy after login
  • Go-MySQL-Driver:

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

Sample Queries

  • Select a single row:

    row := con.QueryRow("select mdpr, x, y, z from sometable where>
    Copy after login
  • Select multiple rows:

    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})
    }
    Copy after login
  • Insert operation:

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

Advantages of Database/sql API

Using the database/sql API in Go for MySQL connectivity offers several benefits:

  • Simplified syntax: The API provides a consistent interface across different databases.
  • Driver interchangeability: Switching between MySQL drivers becomes easy, requiring only import and connection line modifications.

With reliable drivers and a powerful API, connecting to MySQL in Go is a seamless experience.

The above is the detailed content of What's the Best Way to Connect to MySQL from Go Using the `database/sql` API?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template