Skip to Content

Connect Using the SAP HANA Go Interface

0 %
Connect Using the SAP HANA Go Interface

Connect Using the SAP HANA Go Interface

August 16, 2020
Created by
August 16, 2020
Create and debug a Go application that connects to SAP HANA using the SAP HANA client.

You will learn

  • How to install Go
  • How to create a Go application that queries a SAP HANA Database


  • You have completed the first 3 tutorials in this mission.

Go is an open source programming language developed by Google to increase productivity among programmers. For more information, see the Go Documentation.

Step 1: Install Go

The first step is to check if Go is installed, and if so, which version. To do so enter the following command:

go version

If Go is installed, then it will return the currently installed version, such as 1.14. It is required to have at least version 1.8 or later installed.

If it is not installed, download it from Download Go and run the installer and or follow the provided instructions and ensure that Go is in your path.

Go Install

For further details on supported versions, see SAP Note 2939501 - SAP HANA Client Supported Platforms for 2.5 and later.

In order for the shell to recognize that Go has been installed and for any go commands in future steps to be recognized, a new shell window needs to be opened.

Before proceeding, ensure you can create and run an example Go program as described in Test your installation.

Log on to answer question
Step 2: Build the SAP HANA Client Go driver

The SAP HANA Client interface for Go, like the other SAP HANA client interfaces, except for JDBC, makes use of a C library named SQLDBC. The Go driver loads the SQLDBC library named libdbcapiHDB using cgo. For further information on the following steps, consult Build the Go Driver in the SAP HANA Client Interface Programming Reference Guide. A 64-bit gcc compiler is required.

  1. To check if a 64-bit gcc compiler is installed, run the following command:

    gcc --version
    gcc 64-bit

    If it is not installed, download it from Download MinGW and run the installer.

    Note: Set the architecture option to x86_64 and once the install is complete add the bin folder to your path.

    MinGW Installation
  2. Examine the Go environment by running the below command:

    go env

    Notice that GOROOT is set to a location such as C:\go or /usr/local/go. This is the location that the Go SDK is installed to.

    GOPATH is set to a location such as C:\Users\user\go or $HOME/go and defines the root of your workspace which stores your codebase.

  3. Copy the SAP HANA client source code to the Go workspace.

    mkdir %GOPATH%\src
    xcopy C:\SAP\hdbclient\golang\src %GOPATH%\src\ /s
    mkdir -p $HOME/go/src
    cp -r ~/sap/hdbclient/golang/src/SAP $HOME/go/src/

    The results are shown in the screenshot below:
    Result of copying source

  4. Set the CGO_LDFLAGS environment variable to point to the location of libdbcapiHDB library as shown below. Alternatively, set these values in the Microsoft Windows environment variables dialog or on Linux/Mac in the .profile or .bashrc file.

    set CGO_LDFLAGS=c:\sap\hdbclient\libdbcapiHDB.dll
    export CGO_LDFLAGS=$HOME/sap/hdbclient/
    export CGO_LDFLAGS=$HOME/sap/hdbclient/libdbcapiHDB.dylib
  5. Run the install for the driver:

    cd c:\sap\hdbclient\golang\src
    go install SAP/go-hdb/driver
    cd ~/sap/hdbclient/golang/src
    go install SAP/go-hdb/driver

    The result of this should be that a new go-hdb package is installed into $HOME/go

    result of installing the driver
Log on to answer question
Step 3: Create a Go application that queries an SAP HANA database
  1. In a shell, create a folder named go, enter the newly created directory, and open an editor on a file named goQuery.go.

    mkdir %HOMEPATH%\HANAClientsTutorial\go
    cd %HOMEPATH%\HANAClientsTutorial\go
    notepad goQuery.go
    mkdir -p $HOME/HANAClientsTutorial/go
    cd $HOME/HANAClientsTutorial/go
    pico goQuery.go
  2. Add the code below to goQuery.go:
    package main
    import (
      _ "SAP/go-hdb/driver"
    func main() {
      connectString := "hdb://?key=User1UserKey&encrypt=true&sslValidateCertificate=false"
      //connectString := "hdb://"
      fmt.Println("Connect String is " + connectString)
      db, err := sql.Open("hdb", connectString)
      if err != nil {
      defer db.Close()
      rows, err := db.Query("SELECT NAME, ADDRESS from HOTEL.CUSTOMER")
      if err != nil {
      defer rows.Close()
      var lastName string
      var address string
      for rows.Next() {
        err = rows.Scan(&lastName, &address)
        if err != nil {
        fmt.Println(lastName + ": " + address)
      err = rows.Err()
      if err != nil {

    Once the goQuery.go file has been updated, save and close the file.

  3. On Linux or Mac, add the libdbcapiHDB library to the path so that the Go driver can find it:

    export LD_LIBRARY_PATH=~/sap/hdbclient
    export DYLD_LIBRARY_PATH=~/sap/hdbclient

    If the next step to run the application fails with an error message of “Library not loaded”, a workaround to place the libdbcapiHDB library in the same directory as the application executable is available.

    cp ~/sap/hdbclient/libdbcapiHDB.dylib .
  4. Run the application:

    go run goQuery.go
  5. For more information on the API’s used, consult the SAP HANA connection specific properties at Connect to SAP HANA from Go, Go Database/SQL Tutorial, and Package SQL

Log on to answer question
Step 4: Debug the application

Visual Studio Code provides plugins for Go and can be used to debug an application.

  1. If you have not already done so, download Visual Studio Code.

  2. If you have not already done so, in Visual Studio Code, choose File | Add Folder to Workspace, and then add the HANAClientsTutorial folder.

    Open Workspace
  3. Open the file goQuery.go.

    Go Extension

    Visual Studio Code will recognize the go file extension and will suggest installing the Go for Visual Studio Code extension. Click Install.

  4. Place a breakpoint.

  5. Select Run | Start Debugging.

    Notice that the program stops running at the breakpoint that was set.

    Observe the variable values in the leftmost pane. Step through code.


    Note that debugging can also be performed from the command line using Delve.

Congratulations! You have now created and debugged a Go application that connects to and queries an SAP HANA database.

Which of the following statements are true?

Next Steps

Back to top