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
- Anthony Tuininga’s Oracle OpenWorld 2019 session slides: Using the Go Language for Efficient Oracle Database Applications [DEV6708]
- godror
