MacOS安装go-oci8

 

1、instantclient下载basic和sdk

从官网下载instantclinet-basic和skd包:instance-client,并解压缩在固定目录下

2、pkg-config服务,配置oci8.pc

确保pkg-config已安装,如未安装可使用下述命令安装:

brew install pkg-config

配置在$PKG_CONFIG_PATH下新增oci8.pc文件,建议$PKG_CONFIG_PATH采用/usr/local/lib/pkgconfig目录:

  prefixdir=/Users/*my_name*/oracle/instantclient_19_3
  libdir=${prefixdir}
  includedir=${prefixdir}/sdk/include
  Name: OCI
  Description: Oracle database driver
  Version: 19.3
  Libs: -L${libdir} -lclntsh
  Cflags: -I${includedir}

3、go get go-oci8包

go get github.com/mattn/go-oci8

 

4、复制lib到/usr/local/lib下【而非/usr/lib!!】,修改LD_LIBRARY_PATH环境变量,以便程序使用到第三方库

cd /User/*my_name*/oracle/instantclient_19_3
ln libclntsh.dylib.19.1 /usr/local/lib/libclntsh.dylib.19.1
ln libclntsh.dylib /usr/local/lib/libclntsh.dylib
ln libclntsh.dylib.19.1 /usr/local/lib/libclntsh.dylib.19.1
ln libclntshcore.dylib.19.1 /usr/local/lib/libclntshcore.dylib.19.1
ln libociei.dylib.19.1 /usr/local/lib/libociei.dylib.19.1

cat .zshrc

export INSTALL_LIB=/usr/local/lib
export LD_LIBRARY_PATH=/Users/*my_name*/oracle/instantclient_19_3:$INSTALL_LIB

5、测试

package main

import (
	_ "github.com/mattn/go-oci8"
    "database/sql"
    "fmt"
)

func main() {

	db, err := sql.Open("oci8", "usrname/password@IP:PORT/DBNAME")
	if err != nil {
		fmt.Println("connet to db error", err)
	}
	defer db.Close()

	rows, err := db.Query("select 1 + 2 from dual")
	if err != nil {
		fmt.Println(err)
		return
	}

	defer rows.Close()
	for rows.Next() {
		var sum int
		rows.Scan(&sum)
		fmt.Printf("1 + 2 equals: %d\n", sum)
	}
}
# go run main.go 
1 + 2 equals: 3

测试通过。

你可能感兴趣的:(go)