Why let the Pandemic interfere with your hunger for knowledge?
Here’s a learning on Go Language which doesn’t belongs to SAP but which has given several options to connect with databases like HDB, MySQL, etc.
Golang Information, IDE’s and basic Setting:
Go programming language is a statically-typed language with syntax similar to that of C. It provides garbage collection, type safety, dynamic-typing capability, many advanced built-in types such as variable length arrays and key-value maps.
Below are the few important links to learn more about Go Language:
- https://play.golang.org/ (An online editor to practice go language)
Below are the steps to configure and validate Go Language Environment in your local system:
- Download binary release of installation based on your OS from https://golang.org/dl/
- Below is the initial command to check version, environment and helps
- go version
- go env
- go help
- IDE can be used for further go language programming:
1st Hello Go 😊 Language sample Program and save with. go extension:
Output can be seen by using command prompt:
I have used GitBash to execute all Go language programs, below is the command to execute go program file:
Go Language and oData:
Go language has given pre-installed package(net/http) to consume data from oData services.
Below are the details of oData, which I have generated in local HXE server:
To consume this oData service, a deep structure needs to be maintained to map the oData service information into respective fields using a new HTTP request from HTTP package.
Go language has JSON package https://golang.org/pkg/encoding/json/ with different types of functionality to maintain or read information accordingly.
Below is the USL and a request method using net/http package https://golang.org/pkg/net/http/ of Go language.
Then calling the below function to set user id and password to consume the oData result.
I have used below code to skip initial level of proxy setting, as this is local server. This code won’t be required, if we try to consume oData service from any of valid secure server. (I have tried this to consume oData service created in HANA trial cloud server)
Go language uses structures to maintained data which received from oData service. Since the received data into JSON format, which need to be unmarshal (https://golang.org/pkg/encoding/json/) to defined structure. Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.
Below function need to be called to map the received JSON format information to defined structure.
Below is the raw format of the received data based on maintained structure:
Data stored into HXE server:
Go language supports FOR loop (https://golang.org/test/ken/for.go?h=for+loop) in different ways to read range data. I have used to form the received the data into proper readable format:
In next step, I have used template package which is sub-package of HTML (https://golang.org/pkg/html/template/) to form the received information into html table format.
To use a template, a html file should be available and parse into go program to map data into actual HTML file. Below is the template I have used to map received employees information into HTML table:
On executing the GO RUN command in below way, an index.html file will be generating into execution directory.
Final output of index.html file would be:
Go Language and HANA DB Access:
I have gone through below link given by SAP tutorial, to connect HXE server from Go Language.
Addition to above link functionality, I have also tried to fetch record with and without conditions.
HDB has also SQL Commands to fetch records same as other DB. https://github.com/SAP/go-hdb
link has all initial information to use HDB commands in Go language to connectivity, data fetching and other query related functionality.
To do any SQL related activity, go language has provided SQL package https://golang.org/pkg/database/sql/ , which has pre-defined function to perform different type of activities.
In program, declared a getDbUrl function to maintain HXE server detail as returning string value.
Initially used the SQL open function to check the connectivity from Go language to HXE server by using below code:
Below are the three scenarios:
- Program to retrieve the total number of records exist in EMP_MASTER table from HXE server:
- Passing condition to fetch specific record from EMP_MASTER table from HXE server:
- Program to fetch id, First and last name field value for all records from EMP_MASTER table:
Comment line is another query to fetch specific number of records from the table such as UP TO N ROWS in SAP-ABAP 😉
Final output of the all above query would be as below :
Code and Template is available on : github
Lot’s more to learn..
Happy Learning 😊