Per provare a connettere Oracle tramite Go torna utile utilizzare il seguente docker
docker run -d -p 1521:1521 -e ORACLE_PASSWORD=<your password> -v oracle-volume:/opt/oracle/oradata gvenzl/oracle-xe
l'utente e' SYSTEM e service XEPDB1
Per interagire con il db si puo' usare SQLDeveloper
Con la libreria go-ora non e' necessario installare le librerie client di Oracle
package main
import (
"database/sql"
"fmt"
_ "github.com/sijms/go-ora/v2"
)
func sqlOperations(db *sql.DB) {
var queryResultColumnOne string
row := db.QueryRow("SELECT punto_cantiere FROM nodo")
err := row.Scan(&queryResultColumnOne)
if err != nil {
panic(fmt.Errorf("error scanning query result from database into target variable: %w", err))
}
fmt.Println("Risultato query", queryResultColumnOne)
rows, err := db.Query("SELECT punto_cantiere,valore FROM nodo")
if err != nil {
// handle this error better than this
panic(err)
}
defer rows.Close()
for rows.Next() {
var numero float32
var nome string
err = rows.Scan(&nome, &numero)
if err != nil {
// handle this error
panic(err)
}
fmt.Println(numero, nome)
}
// get any error encountered during iteration
err = rows.Err()
if err != nil {
panic(err)
}
}
func GetSqlDBWithPureDriver(dbParams map[string]string) *sql.DB {
connectionString := "oracle://" + dbParams["username"] + ":" +
dbParams["password"] + "@" + dbParams["server"] + ":" +
dbParams["port"] + "/" + dbParams["service"]
db, err := sql.Open("oracle", connectionString)
if err != nil {
panic(fmt.Errorf("error in sql.Open: %w", err))
}
err = db.Ping()
if err != nil {
panic(fmt.Errorf("error pinging db: %w", err))
}
return db
}
var localDB = map[string]string{
"service": "XEPDB1",
"username": "system",
"server": "localhost",
"port": "1521",
"password": "password",
}
func main() {
db := GetSqlDBWithPureDriver(localDB)
defer func() {
err := db.Close()
if err != nil {
fmt.Println("Can't close connection: ", err)
}
}()
sqlOperations(db)
}