How to connect to Oracle in go

himekami picture himekami · Mar 6, 2014 · Viewed 14.7k times · Source

I gather there are two ways to connect to Oracle DB in Go (on windows):

  1. github.com/tgulacsi/goracle
  2. github.com/mattn/go-oci8

But for someone of my level (beginner in open source+golang), those two methods/drivers are awfully tricky.

It's also a burden having to go through all of that for deployment, development on different machines etc. (Also assuming it will work).

Is there a better way to connect to Oracle db in golang or if there is not then can someone explain to me in high level view or any view for that matter that would make this easier?

Pointers would be very much appreciated.

TQ.

Answer

Ricardo Smania picture Ricardo Smania · Sep 19, 2014

If you are still interested, I have been working with Go and Oracle on Windows for a few months now. My favorite driver so far is go-oci8. It is much faster than goracle and seems to be more active.

Some of our applications need to be deployed on computers that we don't have access to. Both native SQL drivers are compiled with the application without the need for any external configuration, so that is a huge plus. The computer will still need Oracle client installed, but that is the only external dependency.

I won't say go-oci8 is production ready yet, but it's stable enough when you know its limitations. One example is that it panics when running on multiple goroutines simultaneously, so if you need that you might want to use a mutex.

I have basically followed this tutorial to install it: https://gist.github.com/mnadel/8678269

The trickiest part was creating oci8.pc corretly. Mine is:

prefix=/devel/target/1.0
exec_prefix=${prefix}
libdir=C:/oracle/instantclient_12_1_64/sdk/lib/msvc
includedir=C:/oracle/instantclient_12_1_64/sdk/include
oralib=C:/oracle/instantclient_12_1_64/sdk/lib/msvc
orainclude=C:/oracle/instantclient_12_1_64/sdk/include
gcclib=c:/MinGW_64/mingw64/lib
gccinclude=c:/MinGW_64/mingw64/lib
glib_genmarshal=glib-genmarshal
gobject_query=gobject-query
glib_mkenums=glib-mkenums
Name: oci8
Version: 12.1
Description: oci8 library
Libs: -L${oralib} -L${gcclib} -loci
Libs.private:
Cflags: -I${orainclude} -I${gccinclude}

Some things might be reduntant, I might try to improve it on a clean machine.

An important thing to have in mind is that you should use the same architecture for Go and the Oracle client. So if you want to use the 64 bit version of Go you will also need the 64 bit version of Oracle. I have both 32 and 64 bit versions of both, and while 64 bit is my default I use bat files to change the necessary paths and environment variables when I need to build a 32 bit version.

It might be worth investing some time to make it work, you will probably get much better performance than using ODBC. I have been using it with somewhat high data volume (queries that fetch 5+ million rows) and it works very well.