Home >Backend Development >Golang >How to solve go build errors for github.com/godror/godror?

How to solve go build errors for github.com/godror/godror?

PHPz
PHPzforward
2024-02-09 08:20:18763browse

如何解决 github.com/godror/godror 的 go 构建错误?

php Xiaobian Yuzai encountered some errors when building the go library using github.com/godror/godror, which troubled him. In order to solve this problem, he conducted in-depth research and attempts and summarized some effective solutions. In this article, we will share his experience and help everyone solve the go build error of github.com/godror/godror, so that everyone can successfully use this library for development work.

Question content

On Mac OS, I am trying to build the following file to run on a Linux machine.

<code>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())
    }
}
</code>

I used the following commands to build:

env GOOS=linux GOARCH=amd64 go build db.go

mistake:

# github.com/godror/godror
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:530:19: undefined: VersionInfo
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:531:19: undefined: VersionInfo
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:532:10: undefined: StartupMode
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:533:11: undefined: ShutdownMode
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:535:31: undefined: Event
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:535:42: undefined: SubscriptionOption
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:535:64: undefined: Subscription
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:536:31: undefined: ObjectType
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:537:59: undefined: Data
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:538:28: undefined: DirectLob
../../../../pkg/mod/github.com/godror/[email&#160;protected]/orahlp.go:538:28: too many errors

I am able to build for Mac OS, but am having trouble building for Linux. Can you help me resolve these errors?

Solution

This cross-compilation error is caused by the github.com/godror/godror package using CGO. To compile the application, you need a valid gcc installation and CGO_ENABLED=1, as Readme. You can try using docker to compile for linux/amd64.

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 file in the current directory.

The above is the detailed content of How to solve go build errors for github.com/godror/godror?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete