linux+go+连接oracle,golang 连接 oracle

Connecting Go to Oracle gives a bit more work than other databases. This is because the Oracle drivers not just link to C code, but also depend on specific environment settings. This post was first written with Windows in mind, but I added a section with Linux specific instructions below. This was tested with the following drivers:

First, remember, you will need the 32-bits version of everything (This works on 64-bits Windows, though). This is because some of the tools you will need are available just as 32-bits for Windows. So, first thing, check if the Go version you have is 32-bits.

Second, you will need to install the oficial MinGW package (again, 32-bits). Don’t forget to add the /bin folder inside the MinGW installation to the PATH.

Now, create a folder called C:\pkg-config, and extract these three ZIP files inside:

Now download the Oracle client (32-bits) and put it on C:\instantclient_11_2. Also download the Oracle SDK (32-bits) and put on C:\instantclient_11_2\sdk.C:\instantclient_11_2 should be added to your PATH.

Now create a folder C:\pkg-config\PKG_CONFIG_PATH, and create a file calledoci8.pc inside with the following content:

prefix=/devel/target/XXXXXXXXXXXXXXXXXXXXXXXXXX

exec_prefix=${prefix}

libdir=C:/instantclient_11_2/sdk/lib/msvc

includedir=C:/instantclient_11_2/sdk/include

glib_genmarshal=glib-genmarshal

gobject_query=gobject-query

glib_mkenums=glib-mkenums

Name: oci8

Version: 11.2

Description: oci8 library

Libs: -L${libdir} -loci

Cflags: -I${includedir}

Add C:\pkg-config\bin to PATH and C:\pkg-config\PKG_CONFIG_PATH to a environment variable called PKG_CONFIG_PATH.

Restart any command prompt to make sure the new environment variables were reconized. If everything is OK, you should be able to properly install one of the Oracle packages:

go install -v github.com/rana/ora

# or

go install -v github.com/mattn/go-oci8

On Linux

This was tested on Ubuntu 16.04, x64 version. First, make sure you installed these tools:

sudo apt-get install build-essential pkg-config

For installing Oracle Client + SDK on Ubuntu, please carefully read and follow this guide on the oficial Ubuntu Wiki. This post won’t cover the SDK installation because it’s very well covered there.

Once you did the installation, create a folder called PKG_CONFIG_PATH in a place you remember later, and then a environment variable called PKG_CONFIG_PATH pointing to this folder. Then, create a file called oci8.pc with the following template (instead of the above for Windows):

prefix=/devel/target/XXXXXXXXXXXXXXXXXXXXXXXXXX

exec_prefix=${prefix}

libdir=/usr/lib/oracle/11.2/client64/lib

includedir=/usr/include/oracle/11.2/client64

glib_genmarshal=glib-genmarshal

gobject_query=gobject-query

glib_mkenums=glib-mkenums

Name: oci8

Description: oci8 library

Libs: -L${libdir} -lclntsh

Cflags: -I${includedir}

Version: 11.2

And try compile to see if everything is OK:

go install -v github.com/mattn/go-oci8

# or

go install -v github.com/rana/ora

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值