Connect Using the SAP HANA Go Interface
- 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.
Go is an open source programming language developed by Google to increase productivity among programmers. For more information, see the Go Documentation.
- Step 1
The first step is to check if Go is installed, and if so, which version. To do so, enter the following command:
ShellCopygo version
If Go is installed, then it will return the currently installed version, such as 1.16.
For further details on supported versions, see SAP Note 3165810 - SAP HANA Client Supported Platforms.
If it is not installed, download it from Download Go, run the installer, follow the provided instructions, and ensure that Go is in your path.
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.
- Step 2
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
usingcgo
. For further information on the following steps, consult Build the Go Driver in the SAP HANA Client Interface Programming Reference Guide. A 64-bitgcc
compiler is required.-
To check if a 64-bit
gcc
compiler is installed, run the following command:ShellCopygcc --version
If it is not installed, on Linux install the System GNU C compiler.
On Windows, it can be downloaded from Download MinGW. Under Mingw-builds, select Installation: Sourceforge to run the installer.
During the install, set the architecture option to
x86_64
.Add the bin folder to your path.
-
Examine the Go environment by running the below command:
ShellCopygo 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. -
Copy the SAP HANA client source code to the Go workspace.
Shell (Windows)Copymkdir %GOPATH%\src xcopy C:\SAP\hdbclient\golang\src %GOPATH%\src\ /s
Shell (Linux or Mac)Copymkdir -p $HOME/go/src cp -r ~/sap/hdbclient/golang/src/SAP $HOME/go/src/
The results are shown in the screenshot below:
-
Set the
CGO_LDFLAGS
environment variable to point to the location oflibdbcapiHDB
library as shown below. The GO111MODULE variable is required in GO versions >= 1.16 and is described further at New module changes in Go 1.16. Preferably, set these values in the Microsoft Windows environment variables dialog or on Linux/Mac in the.profile
or.bash_profile
file so these values persist and can be seen by other applications such as Visual Studio Code.Shell (Windows)Copyset CGO_LDFLAGS=c:\sap\hdbclient\libdbcapiHDB.dll set GO111MODULE=auto
Shell (Linux)Copyexport CGO_LDFLAGS=$HOME/sap/hdbclient/libdbcapiHDB.so export GO111MODULE=auto
Shell (Mac)Copyexport CGO_LDFLAGS=$HOME/sap/hdbclient/libdbcapiHDB.dylib export GO111MODULE=auto
-
Run the install for the driver:
Shell (Windows)Copycd c:\sap\hdbclient\golang\src go install SAP/go-hdb/driver
Shell (Linux or Mac)Copycd ~/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 the GOPATH folder.
-
- Step 3
-
In a shell, create a folder named
go
, enter the newly created directory, and open a file namedgoQuery.go
in an editor.Shell (Microsoft Windows)Copymkdir %HOMEPATH%\HANAClientsTutorial\go cd %HOMEPATH%\HANAClientsTutorial\go notepad goQuery.go
Shell (Linux or Mac)Copymkdir -p $HOME/HANAClientsTutorial/go cd $HOME/HANAClientsTutorial/go pico goQuery.go
- Add the code below to
goQuery.go
:Go CodeCopypackage 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" //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 HOTEL.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.go
file has been updated, save and close the file. -
On Linux or Mac, add the
libdbcapiHDB
library to the path so that the Go driver can find it. You may do this by adding the following lines to the.profile
or.bash_profile
files:Shell (Linux)Copyexport LD_LIBRARY_PATH=~/sap/hdbclient
Shell (Mac)Copyexport 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.
Shell (Mac)Copycp ~/sap/hdbclient/libdbcapiHDB.dylib .
-
Run the application:
ShellCopygo run goQuery.go
-
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
-
- Step 4
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
HANAClientsTutorial
folder. -
Open the file
goQuery.go
.Visual Studio Code will recognize the
go
file extension and will suggest installing the Go for Visual Studio Code extension. Click Install. -
Place 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.
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?
-