https://www.oracle.com/database/technologies/instant-client/downloads.html
这里需要注意的是,Linux版本Instant Client Version19和21对glibc的版本有要求(2.14)
下载basic和sdk packages,下载完成后解压
检查是否安装:
pkg-config --version
Linux安装:
yum install pkgconfig -y
MacOS安装:
brew install pkg-config
https://github.com/mattn/go-oci8
在client目录下面创建oci8.pc文件,文件内容如下,其中xxxxx替换为client的目录:
Linux:
prefix=xxxxx
exec_prefix=${prefix}
includedir=${prefix}/sdk/include
libdir=${prefix}
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: 12.2
MacOS:
prefixdir=xxxxx
libdir=${prefixdir}
includedir=${prefixdir}/sdk/include
Name: OCI
Description: Oracle database driver
Version: 12.2
Libs: -L${libdir} -lclntsh
Cflags: -I${includedir}
.bash_profile中添加,其中xxxxx替换为client的目录:
export LD_LIBRARY_PATH=xxxxx:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=xxxxx
Linux下还需在client目录下创建软链接:
ln -s libclntsh.so.12.1 libclntsh.so
如果还有其他提示找不到动态链接库的话,再创建对应的软链接即可,目前我只遇到这个一个库的报错
func connect(dsn string)(*sql.DB){
log.Println("Launching connection: ", dsn)
db, err := sql.Open("oci8", dsn)
if err != nil {
log.Println("Error while connecting to", dsn)
panic(err)
}
return db
}