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
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 (
    _ "github.com/godror/godror"
func main(){
    db, err := sql.Open("godror", "<your username>/<your password>@service_name")
    if err != nil {
    defer db.Close()
    rows,err := db.Query("select sysdate from dual")
    if err != nil {
        fmt.Println("Error running query")
    defer rows.Close()
    var thedate string
    for rows.Next() {
    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


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.




Join the discussion

Comments ( 11 )
  • 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!
  • Antho Tuesday, September 1, 2020
    How can you pass parameter to a query ?
    Have you some exemple of statement with parameter ?
  • Sergio Tuesday, September 1, 2020
    @Antho, please see Anthony's session slides linked under References above. It has many examples.
  • Ivan Saturday, April 17, 2021
    I'm having troubles when i try to install godror, I'm working in Windows and when i try to install it, the console show me this message. I hope you can help me, I'll appreciate a lot.

    go get github.com/godror/godror
    go: downloading github.com/godror/godror v0.24.4
    go: downloading github.com/go-logfmt/logfmt v0.5.0
    # github.com/godror/godror
    cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
  • Sergio Tuesday, April 27, 2021
    @Ivan - I'm afraid I have no answers for you. Your best bet is to file an issue here on GitHub.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.