Connect to Oracle Database Using go-ora Library in Golang Without Oracle Client
As a Golang developer working with Oracle Database, it’s essential to find an efficient and straightforward way to connect to the database without the hassle of installing an Oracle Client. The go-ora library offers a pure Go solution for connecting to Oracle Database, making it more accessible for developers working in Golang.
In this article, we’ll walk you through how to use the go-ora library to connect to an Oracle Database without requiring an Oracle Client. We’ll explain the process step by step, using a sample function to illustrate the concepts.
Step 1: Import the necessary packages
Before we can use the go-ora library, we need to import the necessary packages. In this case, we need “database/sql” and “fmt” packages from the standard library, as well as “github.com/sijms/go-ora” for the go-ora library itself.
import (
"database/sql"
"fmt"
_ "github.com/sijms/go-ora"
)Step 2: Create the connect function
Now, we’ll create a function called connect that takes the password as an argument and returns a pointer to the sql.DB object and an error. The function sets up the connection to the Oracle Database and tests the connection using the Ping method.
func connect(password string) (*sql.DB, error) {
// Replace these values with your database credentials
username := "admin"
serviceName := "ORACLE"
// The SSH tunnel is forwarding the remote Oracle port to the local port 2525
host := "localhost"
port := 2525
// Set the number of prefetch rows
prefetchRows := 500
// Create the connection string
connStr := fmt.Sprintf(
"oracle://%s:%s@%s:%d/%s?PREFETCH_ROWS=%d",
username, password, host, port, serviceName, prefetchRows,
)
log.Printf("Opening connection to db using go-ora: %v", connStr)
// Connect to the database
db, err := sql.Open("oracle", connStr)
if err != nil {
return nil, err
}
// Test the connection
err = db.Ping()
if err != nil {
db.Close()
return nil, err
}
return db, nil
}Step 3: Understanding the connect function
Let’s break down the connect function to understand how it works:
- Set the Oracle Database credentials (username and serviceName). Replace these values with your credentials.
- Define the host and port. In this example, we use an SSH tunnel to forward the remote Oracle port to the local port 2525.
- Set the number of prefetch rows. This value determines the number of rows fetched from the database in a single round-trip, improving performance for large queries.
- Create the connection string using the provided values.
- Open the connection to the database using
sql.Open("oracle", connStr). - Test the connection using the Ping method. If there’s an error, close the connection and return the error.
Conclusion
The go-ora library makes it easy to connect to an Oracle Database using Golang without the need for an Oracle Client. By following the steps outlined in this article, you can quickly set up a connection and start working with your Oracle Database using Golang. The go-ora library is an excellent alternative to other solutions that require additional dependencies or software installations, making it a valuable resource for Golang developers working with Oracle Databases.
If you enjoy reading medium articles and are interested in becoming a member, I would be happy to share my referral link with you!




