X

Break New Ground

  • November 22, 2018

How to Connect a Go Program to Oracle Database using goracle

Given that we just released Go programming language RPMs on Oracle Linux yum server, I figured it would be a good opportunity to take the goracle driver for a spin on Oracle Linux and connect a Go program to Oracle Database. goracle implements a Go database/sql driver for Oracle Database using ODPI-C (Oracle Database Programming Interface for C)

1. Enable Required Repositories to install Go and Oracle Instant Client

First, install the necessary release RPMs to configure Yum to access the Golang and Oracle Instant Client repositories:

$ sudo yum install -y oracle-golang-release-el7 oracle-release-el7

2. Install Go and Verify

Note that you must install git also so that go get can fetch and build the goracle module.

$ sudo yum -y install git gcc golang
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/opc/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/opc/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/golang"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build280983252=/tmp/go-build -gno-record-gcc-switches"
$ go version
go version go1.12 linux/amd64

3. Install Oracle Instant Client and Add its Libraries to the Runtime Link Path

Oracle Instant Client is available directly from Oracle Linux yum server. If you are deploying applications using Docker, I encourage you to check out our Oracle Instant Client Docker Image.

sudo yum -y install oracle-instantclient18.3-basic
 

Before you can make use of Oracle Instant Client, set the runtime link path so that goracle can find the libraries it needs to connect to Oracle Database.

sudo sh -c "echo /usr/lib/oracle/18.3/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf" 
sudo ldconfig 
 

4. Install the goracle Driver

Following the instructions from the goracle repo on GitHub:

$ go get gopkg.in/goracle.v2

5. Create a Go Program to Test your Connection

Create a file db.go as follows. Make sure you change your connect string.

package main

import (
    "fmt"
    "database/sql"
    _ "gopkg.in/goracle.v2"
)

func main(){

    db, err := sql.Open("goracle", "scott/tiger@10.0.1.127:1521/orclpdb1")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer db.Close()
    
    
    rows,err := db.Query("select sysdate from dual")
    if err != nil {
        fmt.Println("Error running query")
        fmt.Println(err)
        return
    }
    defer rows.Close()

    var thedate string
    for rows.Next() {

        rows.Scan(&thedate)
    }
    fmt.Printf("The date is: %s\n", thedate)
}

6. Run it!

Time to test your program.

$ go run db.go 
The date is: 2019-03-21T17:58:49Z

Conclusion

In this blog post, I showed how you can install the Go programming language and Oracle Instant Client from Oracle Linux yum server and use it together with the goracle Driver to connect a Go program to Oracle Database.

References

 

 

Join the discussion

Comments ( 4 )
  • Carlos Arteaga Tuesday, January 8, 2019
    Excelente tu información,
    Se abre un abanico de posibilidades.
    Muchas gracias y muy agradecido.
  • Lei YANG Friday, April 26, 2019
    Hi, can you give some examples how to access TimesTen database using golang with goracle or something else?
  • Martin Lercher Saturday, July 27, 2019
    What we really need is a go native protocol driver and not another cgo/oci dependent one, b/c otherwise there are so many uncontrollable dependencies on OS, OCI libs, path and so on. Pure DLL hell.
  • Sergio Tuesday, July 30, 2019
    @Martin: I passed on your feedback to our driver team. The benefits of using the Oracle client are that it handles all the security and performance requirements, it's well tested, widely used, and enables us to leverage the existing libraries. On Windows you can put the Oracle client DLLs with any ODPI-C application binary (e.g. goracle) so you don't need to set PATH.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.