Connecting Go Lang to Oracle Database
It seems like more and more people are using Go. With that comes the need to access a database or databases. This blog will show you how to get connected to an Oracle Database and to perform some basic operations using Go.
The first thing you need is to have Go installed. There are a couple of options for you. The first is go download from the Go Lang website, or if you are an Oracle purist they provide some repositories for you and these can be installed using yum.
Next you need to install Oracle Instant Client.
Unzip the contents of the downloaded file. Copy the extracted directory (and it’s contents) to your preferred location and then add the path to this directory to the search PATH. Depending on your configuration and setup, you may need to configure some environment variables. Some people report having to create a ‘.pc’ file and having to change the symlinks for libraries. I didn’t have to do any of this.
The final preparation steps, after installing Go and Oracle Instant Client, is to download the ‘goracle’ package. This package provides a GO database/sql driver for connecting to Oracle Database using ODPI-C. To install the ‘goracle’ package run:
go get gopkg.
in
/goracle
.v2
This takes a few seconds to run. There is no display updates or progress updates when this command is running.
NB: goracle package has been renamed to godror. Everything works the same it’s just the package has been renamed.
See below for the full Go code for connecting to Oracle Database, executing a query and gathering some database information. To this code, with file name ‘ora_db.go’
go run ora_db.go
I’ll now break this code down into steps.
Import the packages that will be used in you application. In this example I’m importing four packages. ‘fmt’ is the formatting package and allows us to write to standard output. the ‘time’ package allows us to capture and print some date, time and how long things take. The ‘database/sql’ package to allow SQL execution and is needed for the final package ‘goracle’.
import ( "fmt" "time" "database/sql" goracle "gopkg.in/goracle.v2" )
Next we can define the values needed for connecting the Oracle Database. These include the username, password, the host string and the database name.
username := "odm_user"; password := "odm_user"; host := "....."; database := "....";
Now test the database connection. This doesn’t actually create a connection. This is deferred until you run the first command against the database. This tests the connection
db, err := sql.Open("goracle", username+"/"+password+"@"+host+"/"+database) if err != nil { fmt.Println("... DB Setup Failed") fmt.Println(err) return } defer db.Close()
If an error is detected, the error message will be printed and the application will exit (return). the ‘defer db.Close’ command sets up to close the connection, but defers it to the end of the application. This allows you to keep related code together and avoid having to remember to add the close command at the end of your code.
Now force the connection to open using a Ping command
if err = db.Ping(); err != nil { fmt.Println("Error connecting to the database: %s\n", err) return }
Our database connection is now open!
The ‘goracle’ package allows us to get some metadata about the connection, such as details of the client and server configuration. Here we just gather the details of what version of the database we are connected to. The Oracle Database I’m using is 18c Extreme Edition host on Oracle Cloud.
var serverVersion goracle.VersionInfo serverVersion, err = goracle.ServerVersion(db); if err != nil { fmt.Printf("Error getting Database Information: %s\n", err) return } fmt.Println("DB Version : ",serverVersion)
First we define the variable used to store the server details in. This is defined with data type as specified in the ‘goracle’ package. Then gather the server version details, check for an error and finally print out the details.
To execute a query, we define the query (dbQuery) and then use the connection (db) to run this query (db.Query). The variable ‘rows’ points to the result set from the query. Then defer the closing of the results set point. We need to keep this results set open, as we will parse it in the next code segment.
dbQuery := "select table_name from user_tables where table_name not like 'DM$%' and table_name not like 'ODMR$%'" rows, err := db.Query(dbQuery) if err != nil { fmt.Println(".....Error processing query") fmt.Println(err) return } defer rows.Close()
To parse the result set, we can use a FOR loop. Before the loop we define a variable to contain the value returned from the result set (tableName). The FOR loop will extract each row returned and assign the value returned to the variable tableName. This variable is then printed.
var tableName string for rows.Next() { rows.Scan(&tableName) fmt.Println(tableName) }
That’s it.
We have connected to Oracle Database, retrieved the version of the database, executed a query and processed the result set.
Here is the full code and the output from running it.
package main import ( "fmt" "time" "database/sql" goracle "gopkg.in/goracle.v2" ) func main(){ username := "odm_user"; password := "odm_user"; host := "....."; database := "...."; currentTime := time.Now() fmt.Println("Starting at : ", currentTime.Format("03:04:05:06 PM")) fmt.Println("... Setting up Database Connection") db, err := sql.Open("goracle", username+"/"+password+"@"+host+"/"+database) if err != nil { fmt.Println("... DB Setup Failed") fmt.Println(err) return } defer db.Close() fmt.Println("... Opening Database Connection") if err = db.Ping(); err != nil { fmt.Println("Error connecting to the database: %s\n", err) return } fmt.Println("... Connected to Database") var serverVersion goracle.VersionInfo serverVersion, err = goracle.ServerVersion(db); if err != nil { fmt.Printf("Error getting Database Information: %s\n", err) return } fmt.Println("DB Version : ",serverVersion) dbQuery := "select table_name from user_tables where table_name not like 'DM$%' and table_name not like 'ODMR$%'" rows, err := db.Query(dbQuery) if err != nil { fmt.Println(".....Error processing query") fmt.Println(err) return } defer rows.Close() fmt.Println("... Parsing query results") var tableName string for rows.Next() { rows.Scan(&tableName) fmt.Println(tableName) } fmt.Println("... Closing connection") finishTime := time.Now() fmt.Println("Finished at ", finishTime.Format("03:04:05:06 PM")) }
One thought on “Connecting Go Lang to Oracle Database”
Comments are closed.
August 5, 2019 at 2:05 am
[…] data for inserting into a table in an Oracle Database. I’ve had some previous blog posts on how to setup and connecting to an Oracle Database, and another on retrieving data from an Oracle Database and the importance of setting the Array […]
LikeLike