Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
102 lines (74 sloc) 2.83 KB


go-dbi is a database independent interface for accessing databases from the Go language.




Perl's DBI interface is a popular model for database connectivity in modern programming languages. go-dbi brings a flavour of this convenient API to the Go programming language.

If you're at all familiar with Perl DBI, the PEAR::DB or MDB2 PHP libraries or Ruby/DBI, you should feel right at home with go-dbi.


  1. Make sure you have a working Go environment. See the install instructions.
  2. Ensure goinstall is on your $PATH.
  3. Install go-dbi: $ goinstall

Alternatively, you can clone the git repository & build it using make.


This example assumes you have both go-dbi and the go-dbd-mysql driver installed.

package main

import (
    dbi ""
    // important: import all drivers that need to be supported by your
    //            application!
    _ ""

func main() {
    conn, err := dbi.Connect("mysql://root@localhost/somedatabase")
    if err != nil {
        fmt.Printf("error: unable to connect to the database: %s\n", err.String())
    defer conn.Close()

    err = conn.Execute("INSERT INTO users (username) VALUES ('tom')")
    if err != nil {
        fmt.Printf("error: unable to insert user: %s\n", err.String())

    rs, err := conn.Query("SELECT username FROM users WHERE username='tom'")
    if err != nil {
        fmt.Printf("error: unable to fetch users: %s\n", err.String())
    defer rs.Close()

    for rs.Next() {
        // You can scan the current row of values like so ...
        var username string
        err = rs.Scan(&username)
        if err != nil {
            fmt.Printf("error: %s\n", err.String())
        } else {
            fmt.Printf("rs.Scan(&username): %s\n", username)

        // ... or scan by column name.
        err = rs.NamedScan("username", &username)
        if err != nil {
            fmt.Printf("error: %s\n", err.String())
        } else {
            fmt.Printf("rs.Scan(\"username\", &username): %s\n", username)


This software is licensed under the terms of the MIT License.


Please log defects and feature requests using the issue tracker on github.


go-dbi was written by Tom Lee.

Follow me on Twitter or LinkedIn.

Jump to Line
Something went wrong with that request. Please try again.