首先,需要安装第三方数据库驱动包Godror,其中包含了驱动以及支持标准Go语言SQL包规范的函数调用及对象。使用下面的go get方法安装即可。
go get -v github.com/godror/godror
代码中的包导入(即import) 时,要下划线导入gordor包,下划线导入的作用是仅执行其中的init函数,加载驱动。
_ "github.com/godror/godror"
之后编写程序即可编译通过(如何编写代码请参看godror包的说明,在pkg.go.dev查看即可),但如需正确执行访问Oracle数据库的代码,还需要安装oracle客户端。
到下面地址下载,可能需要Oracle的账户,直接注册一个即可。
https://www.oracle.com/database/technologies/instant-client/downloads.html
还需要安装vc运行库,
https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads
这里是最新的vc运行时大全,下载最全的最新的即可。
最后是原文的Oracle客户端安装说明,仅针对Windows下有意义,供大家参考吧。最主要是把下载的客户端的路径加入PATH环境变量中。
Instant Client Installation for Microsoft Windows 64-bit
See the Instant Client Home Page for more information about Instant Client packages.
Client-server version interoperability is detailed in Doc ID 207303.1. For example, Oracle Call Interface 19, 18 and 12.2 can connect to Oracle Database 11.2 or later. Some tools may have other restrictions.
Download the appropriate Instant Client packages for your platform. All installations require the Basic or Basic Light package.
Unzip the packages into a single directory such as C:\oracle\instantclient_19_3
Add this directory to the PATH environment variable. If you have multiple versions of Oracle libraries installed, make sure the new directory occurs first in the path. Restart any terminal windows or otherwise make sure the new PATH is used by your applications.
Download and install the correct Visual Studio Redistributable from Microsoft. Instant Client 19 requires the Visual Studio 2017 redistributable. Instant Client 18 and 12.2 require the Visual Studio 2013 redistributable. Instant Client 12.1 requires the Visual Studio 2010 redistributable.
If you intend to co-locate optional Oracle configuration files such as tnsnames.ora, sqlnet.ora, ldap.ora, or oraaccess.xml with Instant Client, then create a subdirectory such as C:\oracle\instantclient_19_3\network\admin
This is the default Oracle client configuration directory for applications linked with this Instant Client.
Alternatively, Oracle client configuration files can be put in another, accessible directory. Then set the environment variable TNS_ADMIN to that directory name.
Start your application.
ODBC users should follow the ODBC Installation Instructions.