X

Break New Ground

  • March 3, 2019

How to Connect a Go Program to Oracle Database using godror

Note: This blog post was updated from the original to reflect the new name for the Go Driver for Oracle Database: godror, ormerly known as goracle.

Oracle has offered Go programming language RPMs on Oracle Linux yum server, for a while now. In this post, I demonstrate how to use GO Driver for Oracle Database on Oracle Linux and connect a Go program to Oracle Database. godror 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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/opc/.cache/go-build"
GOENV="/home/opc/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/opc/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/golang"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build591934672=/tmp/go-build -gno-record-gcc-switches"
$ go version
go version go1.13.8 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 godror Driver

Following the instructions from the goracle repo on GitHub:

$ go get github.com/godror/godror

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"
    _ "github.com/godror/godror"
)
 
func main(){
 
    db, err := sql.Open("godror", "<your username>/<your password>@service_name")
    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: 2020-03-02T17:43:58Z

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 godror Driver to connect a Go program to Oracle Database.

References

 

 

Join the discussion

Comments ( 7 )
  • 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.
  • Jan Willem Teunisse Saturday, February 29, 2020
    Your article helped me to connect to my XE oracle database, but the "gopkg.in/goracle.v2" package is no longer in use. Instead we have to use "github.com/godror/godror".
    Maybe you can edit your article in order to avoid future readers this mistake. Thanks in advance.
  • Sergio Monday, March 2, 2020
    @Jan - you are correct. The blog post was due for an update to reflect this. I've just done so. Thanks for your comment.
  • Oscar Tuesday, March 3, 2020
    Muchas gracias por tu post, está muy fácil de entender!
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.