Ready to get started?

Learn more about the CData ODBC Driver for MarkLogic or download a free trial:

Download Now

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

Use the CData ODBC Driver for MarkLogic and unixODBC to create a simple Go app with live connectivity to MarkLogic 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 MarkLogic and unixODBC you are able write applications with connectivity to live MarkLogic data. This article will walk you through the process of installing the ODBC Driver for MarkLogic, configuring a connection using the unixODBC Driver Manager, and creating a simple Go application to work with MarkLogic 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 MarkLogic ...

List the Defined Data Source(s)

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

To use the CData ODBC Driver for MarkLogic 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.marklogic.ini), which can be found in the lib folder in the installation location (typically /opt/cdata/cdata-odbc-driver-for-marklogic), as follows:


... [Driver] DriverManagerEncoding = UTF-16

Modifying the DSN


Set User, Password, and Server to the credentials for the MarkLogic account and the address of the server you want to connect to. You should also specify the REST API Port if you want to use a specific instance of a REST Server.

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

[CData MarkLogic Source] Driver = CData ODBC Driver for MarkLogic Description = My Description User = 'myusername' Password = 'mypassword' Server = 'http://marklogic'


Creating a Simple Go App for MarkLogic Data

With the Driver Manager installed and the DSN configured, you are ready to create a simple Go application to work with your MarkLogic 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 MarkLogic data and displays the results. Create the directory $GOPATH/src/cdata-odbc-marklogic 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 MarkLogic Source") if err != nil { log.Fatal(err) } var ( name string totaldue string ) rows, err := db.Query("SELECT Name, TotalDue FROM Customer WHERE Id = ?", "1") if err != nil { log.Fatal(err) } defer rows.Close() for rows.Next() { err := rows.Scan(&name, &totaldue) if err != nil { log.Fatal(err) } fmt.Println(name, totaldue) } 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 MarkLogic data:

$ ./cdata-odbc-marklogic

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