Overview
You will learn
- How to install Go
- How to create a Go application that queries a SAP HANA Database
Prerequisites
- You have completed the first 3 tutorials in this mission.
Steps
Intro
Go is an open-source programming language developed by Google to increase productivity among programmers. For more information, see the Go Documentation.
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.22.1.
For further details on supported versions, see SAP Note 3165810 - SAP HANA Client Supported Platforms.
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.
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 Set Up Your Application to Use the Go Driver Package in the SAP HANA Client Interface Programming Reference Guide. A 64-bit gcc compiler is required.
To check if a 64-bit
gcccompiler is installed, run the following command:Shellgcc --version
gcc 64-bit
Examine the Go environment by running the below command:
Shellgo envNotice that GOROOT is set to a location such as
C:\goor/usr/lib64/go/1.22. This is the location that the Go SDK is installed to.GOPATH is set to a location such as
C:\Users\user\goor$HOME/goand defines the root of your workspace which stores your codebase.Set the
CGO_LDFLAGSenvironment variable to point to the location of thelibdbcapiHDBlibrary as shown below and set the appropriate library path variable if needed.
- Go to the driver folder and create a go module.

The contents of the HANA Client folder is not writeable so you may need to change the permissions on the driver folder or copy files to a new location.
- In a shell, create a folder named
go, enter the newly created directory, and open a file namedgoQuery.goin an editor.
Add the code below to
goQuery.go:Gopackage main import ( "fmt" "database/sql" "log" _ "SAP/go-hdb/driver" ) func main() { //Option 1, retrieve the connection parameters from the hdbuserstore //host, port, user name and password come from the hdbuserstore key USER1UserKey connectString := "hdb://?key=USER1UserKey&encrypt=true&sslValidateCertificate=false&sessionVariable:APPLICATION=goQuery.go" //Option 2, specify the connection parameters //connectString := "hdb://User1:Password1@999deec0-ccb7-4a5e-b317-d419e19be648.hana.prod-us10.hanacloud.ondemand.com:443?encrypt=true&sslValidateCertificate=false" //encrypt and sslValidateCertificate should be true for HANA Cloud connections //As of SAP HANA Client 2.6, connections on port 443 enable encryption by default fmt.Println("Connect String is " + connectString) db, err := sql.Open("hdb", connectString) if err != nil { log.Fatal(err) return } defer db.Close() rows, err := db.Query("SELECT NAME, ADDRESS from HOTELS.CUSTOMER") if err != nil { log.Fatal(err) } defer rows.Close() var lastName string var address string for rows.Next() { err = rows.Scan(&lastName, &address) if err != nil { log.Fatal(err) } fmt.Println(lastName + ": " + address) } err = rows.Err() if err != nil { log.Fatal(err) } }Once the
goQuery.gofile has been updated, save and close the file.The
sessionVariable:APPLICATIONparameter sets a custom application name for the connection. This value can be viewed in theCLIENT_APPLICATIONcolumn of theM_CONNECTIONSsystem view and may also appear in SAP HANA telemetry data.Create another go module and modify its contents:
Add the code below to
go.modunder the go version line:Make sure the path to the driver folder is correct and make any necessary changes.

Place the dbcapi shared library files in /HANAClientsTutorial/go directory by copying or dragging them into that folder. Then, run the application:
Shellgo run goQuery.go
Result
For more information on the API’s used, consult the SAP HANA connection specific properties at Go Connection Properties, Go Database/SQL Tutorial, and Package SQL.
Visual Studio Code provides plugins for Go and can be used to debug an application.
If you have not already done so, download Visual Studio Code.
If you have not already done so, in Visual Studio Code, choose File | Add Folder to Workspace, and then add the
HANAClientsTutorialfolder.
Open the workspace Open the file
goQuery.go.
Go extension install Visual Studio Code will recognize the
gofile extension and will suggest installing the Go for Visual Studio Code extension. Click Install.Check whether a
launch.jsonfile has been created. If not, go to the Run and Debug tab and select “Create a launch.json file.”
Create a launch Open the file
launch.jsonand add the following codeGo"env": { "CGO_LDFLAGS": "./libdbcapiHDB.dylib" }
Go JSON file Change
./libdbcapiHDB.dylibto the correct file for your operating system: uselibdbcapiHDB.dylibon macOS,libdbcapiHDB.dllon Windows, orlibdbcapiHDB.soon Linux. Also ensure the dbcapi file has been copied or moved into this folder.Place a breakpoint.

Set a breakpoint 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.

Breakpoint 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.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.




