On a Mac Os, I'm trying to build the below file to run in a linux machine.
package main
import (
"context"
"database/sql"
_ "github.com/godror/godror"
)
func main() {
dsn := "user/password@host:port/sid"
// Open a connection to the Oracle database
db, err := sql.Open("godror", dsn)
if err != nil {
panic(err.Error())
}
defer db.Close()
// Test the database connection
ctx := context.Background()
err = db.PingContext(ctx)
if err != nil {
panic(err.Error())
}
query := "SELECT * FROM table"
rows, err := db.QueryContext(ctx, query)
if err != nil {
panic(err.Error())
}
defer rows.Close()
if err := rows.Err(); err != nil {
panic(err.Error())
}
}
I'm using the below command to build:
env GOOS=linux GOARCH=amd64 go build db.go
Errors:
# github.com/godror/godror
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:530:19: undefined: VersionInfo
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:531:19: undefined: VersionInfo
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:532:10: undefined: StartupMode
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:533:11: undefined: ShutdownMode
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:535:31: undefined: Event
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:535:42: undefined: SubscriptionOption
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:535:64: undefined: Subscription
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:536:31: undefined: ObjectType
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:537:59: undefined: Data
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:538:28: undefined: DirectLob
../../../../pkg/mod/github.com/godror/godror@v0.39.2/orahlp.go:538:28: too many errors
I am able to build for Mac OS, but having issues to build for linux. Could you help me resolve these errors?
This cross-compilation error is due to the usage of CGO by the github.com/godror/godror
package. For compiling the application you need a valid gcc installation and CGO_ENABLED=1, as mentioned in the readme of the package.
You can try compiling for linux/amd64 using docker.
Example:
DOCKER_DEFAULT_PLATFORM=linux/amd64 docker run --rm -v "$PWD":/app -w /app golang:1.21 go build -v db.go
This will build the application in a docker container and save the executable in the current directory.