Ready to get started?


Download Now

Write a Simple Go Application to work with MariaDB Data on Linux

Use the CData ODBC Driver for MariaDB and unixODBC to create a simple Go app with live connectivity to MariaDB data.

Go is an open source programming language that enables you to easily build software on Linux/UNIX machines. When Go is paired with the ODBC Driver for MariaDB and unixODBC you are able write applications with connectivity to live MariaDB data. This article will walk you through the process of installing the ODBC Driver for MariaDB, configuring a connection using the unixODBC Driver Manager, and creating a simple Go application to work with MariaDB data.

Using the CData ODBC Drivers on a Unix/Linux Machine


Installing the Driver Manager



$ apt-get install unixODBC unixODBC-dev


$ yum install unixODBC unixODBC-devel


$ odbcinst -j



Installing the Driver






List the Registered Driver(s)

$ odbcinst -q -d CData ODBC Driver for MariaDB ...

List the Defined Data Source(s)

$ odbcinst -q -s CData MariaDB Source ...

To use the CData ODBC Driver for MariaDB with unixODBC, you need to ensure that the driver is configured to use UTF-16. To do so, edit the INI file for the driver (cdata.odbc.mariadb.ini), which can be found in the lib folder in the installation location (typically /opt/cdata/cdata-odbc-driver-for-mariadb), as follows:


... [Driver] DriverManagerEncoding = UTF-16

Modifying the DSN



/etc/odbc.ini or $HOME/.odbc.ini

[CData MariaDB Source] Driver = CData ODBC Driver for MariaDB Description = My Description User = myUser Password = myPassword Database = NorthWind Server = myServer Port = 3306


Creating a Simple Go App for MariaDB Data

With the Driver Manager installed and the DSN configured, you are ready to create a simple Go application to work with your MariaDB data. To start, install a Go driver for ODBC databases. While there are several options available, this article will use the odbc driver found at https://github.com/alexbrainman/odbc.

Installing odbc on Linux


export GOPATH=$HOME/golang/go


$ go get github.com/alexbrainman/odbc


Sample Go Application

The sample application issues a simple SQL SELECT query for MariaDB data and displays the results. Create the directory $GOPATH/src/cdata-odbc-mariadb and create a new Go source file, copying the source code from below.


package main import ( _ "github.com/alexbrainman/odbc" "database/sql" "log" "fmt" ) func main() { db, err := sql.Open("odbc", "DSN=CData MariaDB Source") if err != nil { log.Fatal(err) } var ( shipname string shipcity string ) rows, err := db.Query("SELECT ShipName, ShipCity FROM Orders WHERE ShipCountry = ?", "USA") if err != nil { log.Fatal(err) } defer rows.Close() for rows.Next() { err := rows.Scan(&shipname, &shipcity) if err != nil { log.Fatal(err) } fmt.Println(shipname, shipcity) } err = rows.Err() if err != nil { log.Fatal(err) } defer db.Close() }


$ go build

After the application builds, you will be able to execute the application, displaying your MariaDB data:

$ ./cdata-odbc-mariadb

At this point, you have a simple Go application for working with MariaDB data. From here, you can easily expand the application, adding deeper read/write functionality through familiar SQL queries.