Golang连贯Oracle 须要装置Oracle Full Client或Instant Client

Oracle的Instant Client套件下载地址

#抉择Instant Client for Linux x86-64下载包如下:
instantclient-basic-linux.x64-21.1.0.0.0.zip
instantclient-sqlplus-linux.x64-21.1.0.0.0.zip
instantclient-sdk-linux.x64-21.1.0.0.0.zip

#都解压到 instantclient_21_1 这个目录,间接解压会解压到instantclient_21_1目录
unzip instantclient-basic-linux.x64-21.1.0.0.0.zip 
unzip instantclient-sdk-linux.x64-21.1.0.0.0.zip
unzip instantclient-sqlplus-linux.x64-21.1.0.0.0.zip

#打印instantclient_21_1目录的门路
pwd
/home/oracle/instantclient_21_1
装置pkg-config

CentOS自带了pkg-config,这里不在装置

#打印pkg-config版本
pkg-config --version 
创立配置文件oci8.pc

vim /home/oracle/oci8.pc
oci8.pc文件内容如下:

prefix=/home/oracle/instantclient_21_1
libdir=${prefix}
includedir=${prefix}/sdk/include/

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: 21.1
配置环境变量

vim /etc/profile

在最初增加如下环境变量

# Oracle的OCI套件
export LD_LIBRARY_PATH=/home/oracle/instantclient_21_1
# oci8.pc文件所在门路
export PKG_CONFIG_PATH=/home/oracle

使配置失效
source /etc/profile
运行Golang连贯Oracle报错一:
# github.com/mattn/go-oci8
In file included from ../github.com/mattn/go-oci8/c_helpers.go:3:0:
./oci8.go.h:1:17: fatal error: oci.h: No such file or directory
 #include <oci.h>
                 ^
compilation terminated.

是因为配置的环境变量,oci8.pc文件所在门路不对,找不到oci8.pc文件导致的
留神这个门路配置对
export PKG_CONFIG_PATH=/home/oracle

运行Golang连贯Oracle报错二:
dyld: Library not loaded: @rpath/libclntsh.dylib.19.1
  Referenced from: /var/folders/_f/n3km_s7j6_9f8qzpkf9w395h0000gn/T/go-build114883360/b001/exe/oracleoci8
  Reason: image not found

增加软链接,解决报错

ln -s /home/oracle/instantclient_21_1/libclntsh.so.21.1 /usr/lib/libclntsh.so
ln -s /home/oracle/instantclient_21_1/libocci.so.21.1 /usr/lib/libocci.so
ln -s /home/oracle/instantclient_21_1/libociei.so /usr/lib/libociei.so
ln -s /home/oracle/instantclient_21_1/libnnz21.so /usr/lib/libnnz12.so