猿问

无法使用 oracle db 在 golang 中构建应用程序

我是 go lang 开发的初学者,我尝试从 go 服务器连接云 oracle 数据库(使用 'github.com/godror/godror' 包)。我安装了 Oracle 即时客户端并设置了环境路径。


Go 服务器正在运行并且能够建立连接和查询。但是当我构建一个生产应用程序时出现了这些错误。


命令:env GOOS=linux GOARCH=amd64 go build -o odb db_main.go


../github.com/godror/godror/orahlp.go:452:53: undefined: VersionInfo

../github.com/godror/godror/orahlp.go:461:53: undefined: VersionInfo

../github.com/godror/godror/orahlp.go:479:19: undefined: VersionInfo

../github.com/godror/godror/orahlp.go:480:19: undefined: VersionInfo

../github.com/godror/godror/orahlp.go:481:30: undefined: ObjectType

../github.com/godror/godror/orahlp.go:482:31: undefined: Event

../github.com/godror/godror/orahlp.go:482:42: undefined: SubscriptionOption

../github.com/godror/godror/orahlp.go:482:64: undefined: Subscription

../github.com/godror/godror/orahlp.go:483:10: undefined: StartupMode

../github.com/godror/godror/orahlp.go:484:11: undefined: ShutdownMode

../github.com/godror/godror/orahlp.go:484:11: too many errors```




My server code(db_main.go)

```package main

import (

    "database/sql"

    "encoding/json"

    "fmt"

    "log"

    "net/http"

    "time"



_ "github.com/godror/godror"

)



//  VersionInfo,ObjectType,Event,SubscriptionOption,Subscription,StartupMode,ShutdownMode

var db *sql.DB


func main() {

    var err error


    db, err = sql.Open("godror", "xyz/abc@13.43.11.8:1521/sampledb")

    if err != nil {

        log.Println("Error")

        fmt.Println(err)

        return

    }

    defer db.Close()


    port := ":8000"

    http.HandleFunc("/insert", InsertData)

    http.HandleFunc("/list", GetList)

    log.Println("Tranzo-Shahi Oracle DB running in ", port)

    httpErr := http.ListenAndServe(port, nil)

    if httpErr != nil {

        log.Println("Tranzo-Shahi Oracle DB Error: ", httpErr.Error())

    }

}


幕布斯6054654
浏览 402回答 2
2回答

函数式编程

不幸的是,原始问题没有提到构建运行的位置。知道这一点很重要。当您尝试交叉编译代码时,godror文档指出了可能出现的问题。在他们的文档中:Cgo is required, so cross-compilation is hard, and you cannot set CGO_ENABLED=0!我建议在 Linux 机器上运行你的构建。(验证它是否安装了gcc。)运行您的构建env CGO_ENABLED=1 go build -o odb

慕虎7371278

运行env GOOS=linux GOARCH=amd64 go build -o odb并且不指定任何文件。如果您确实指定了一个,db_main.go那么您需要指定必须包含在构建中的包中的所有文件。
随时随地看视频慕课网APP

相关分类

Go
我要回答