Linux 环境 安装 oracle-instantclient 下载
解压 1 2 3 4 5 6 7 8 unzip -ojd /usr/local instantclient-basiclite-linux.x64-12.2.0.1.0.zip or unzip -ojd /usr/local instantclient-basic-linux.x64-12.2.0.1.0.zip ln -snf /usr/local/instantclient_12_2/libclntsh.so.12.1 /usr/local/instantclient_12_2/libclntsh.sounzip -ojd /usr/local instantclient-sdk-linux.x64-12.2.0.1.0.zip
设置 LD_LIBRARY_PATH 1 export LD_LIBRARY_PATH =$LD_LIBRARY_PATH :/usr/local/instantclient_12_2
oci8.pc
修改 pc 文件 (可以参考 github.com/mattn/go-oci8/README.MD)
1 2 3 4 5 6 7 8 9 prefix =/usr/local/instantclient_12_2includedir =${prefix} /sdk/includelibdir =${prefix} Name: oci8 Description: Oracle Instant Client Version: 12.2Cflags: -I${includedir} Libs: -L${libdir} -lclntsh
重新配置环境变量 PKG_CONFIG_PATH 目录,放入 oci8.pc
export PKG_CONFIG_PATH=xxx
放入系统路径, /usr/local/lib/pkgconfig/
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
检查开发环境 1 2 3 4 ➜ pkg-config --cflags oci8 -I/usr/local/instantclient_12_2/sdk/include/
windows 环境 安装 oracle-instantclient 下载
Oracle 官网 下载 zip 包并解压
instantclient-basiclite-windows.x64-12.2.0.1.0.zip
instantclient-basic-windows.x64-12.2.0.1.0_2.zip
instantclient-sdk-windows.x64-12.2.0.1.0.zip
解压 1 2 3 4 5 到C盘 解压 instantclient-basiclite-windows.x64-12.2.0.1.0.zip 解压 instantclient-sdk-windows.x64-12.2.0.1.0.zip
设置 oracle 环境 1 2 3 4 5 ORACLE_HOME = C:\instantclient_12_2 TNS_ADMIN = %ORACLE_HOME%\ADMIN NLS_LANG = SIMPLIFIED CHINESE_CHINA.AL32UTF8 or NLS_LANG = SIMPLIFIED CHINESE_CHINA.ZHS16GBK
安装 Mingw-w64 http://sourceforge.net/projects/mingw-w64/
8.1.0 posix seh x86_64-8.1.0-release-posix-seh-rt_v6-rev0
直接下载解压即可,解压的目录 %MINGW64_HOME% ,将 %MINGW64_HOME%\bin 配置在环境变量 Path 中 然后命令行输入 gcc -v 打印
1 2 3 4 5 6 7 8 9 10 $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=C:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib ' Thread model: posix gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
配置全局 make 进入mingw64 的安装目录,新建文件 make.bat 内容为
1 2 @echo offC:\mingw64 \bin\mingw32 -make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9
1 2 3 4 5 6 7 8 $ make -v GNU Make 4.2.1 Built for x86_64-w64-mingw32 Copyright (C) 1988-2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
pkg-config 新建 C:\mingw64\lib\pkg-config\oci8.pc
1 2 3 4 5 6 7 8 9 10 prefix =C:/instantclient_12_2/instantclient_12_2exec_prefix =${prefix} libdir =${exec_prefix} includedir =${prefix} /sdk/include/Name: oci8 Description: oci8 library Version: 12.2 Libs: -L${libdir} -loci Cflags: -I${includedir}
环境变量
1 PKG_CONFIG_PATH = C:\mingw64\lib\pkg-config
安装 pkg-config.exe
https://pkg-config.freedesktop.org/releases/
https://stackoverflow.com/questions/1710922/how-to-install-pkg-config-in-windows
http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/
C:\mingw64\bin\pkg-config.exe
检查开发环境 1 2 pkg-config --cflags oci8 -IC :/instantclient_12_2/sdk/include/
示例代码 sql.go 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 cat >sql.go <<\EOFpackage main import ( "database/sql" "fmt" "log" "os" _ "github.com/mattn/go-oci8" ) func main () { if len(os.Args) != 2 { log.Fatalln(os.Args[0] + " user/password@host:port/sid" ) } db, err := sql.Open("oci8" , os.Args[1]) if err != nil { log.Fatalln(err) } defer db.Close() rows, err := db.Query("select user from dual" ) if err != nil { log.Fatalln(err) } defer rows.Close() for rows.Next () { var data string rows.Scan(&data) fmt.Println(data) } if err = rows.Err(); err != nil { log.Fatalln(err) } } EOF
database/sql 参考 Go database/sql tutorial