dongxia5394 2019-01-14 08:52
浏览 155
已采纳

如何将连接参数db传递给main.go?

I have trouble with my current works. I build an apps using beego framework, Im new in golang.

First, I build other package called utils, and from that package I write some codes to access to my databases

func InitFirebird() {
    var (
        dbDriver   = beego.AppConfig.String("DB_CONNECTION")
        dbUsername = beego.AppConfig.String("DB_USERNAME")
        dbPassword = beego.AppConfig.String("DB_PASSWORD")
        dbServer   = beego.AppConfig.String("DB_HOST")
        // dbPort     = beego.AppConfig.String("DB_PORT")
        dbFileName = beego.AppConfig.String("DB_DATABASE")
    )
    conn, _ := sql.Open(dbDriver, dbUsername+":"+dbPassword+"@"+dbServer+"/"+dbFileName)
    defer conn.Close()
}

After that, I go to my main.go and setting up my init function and main function like this:

func init() {
    utils.InitFirebird()
}

func main() {
    if beego.BConfig.RunMode == "dev" {
        beego.BConfig.WebConfig.DirectoryIndex = true
        beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
        }

    var n int

    conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
    fmt.Println("Relations count=", n)

    beego.Run()
}

When I startover my apps, its error and provide me this message :

\main.go:23:2: undefined: conn

How can I resolve this?

Anyhelp will appreciate

  • 写回答

1条回答 默认 最新

  • douliang1900 2019-01-14 09:03
    关注

    First, if you want to access something from another package, it has to be exported. In Go, if you want to export something you name it with first letter as uppercase (in your case, it should be Conn instead of conn).

    Second, when you use defer it will get executed when the function returns. In your case, it returns immediately, so the connection is closed immediately.

    Solution:

    var Conn *sql.DB
    func InitFirebird() {
        var (
            dbDriver   = beego.AppConfig.String("DB_CONNECTION")
            dbUsername = beego.AppConfig.String("DB_USERNAME")
            dbPassword = beego.AppConfig.String("DB_PASSWORD")
            dbServer   = beego.AppConfig.String("DB_HOST")
            // dbPort     = beego.AppConfig.String("DB_PORT")
            dbFileName = beego.AppConfig.String("DB_DATABASE")
        )
        Conn, _ = sql.Open(dbDriver, dbUsername+":"+dbPassword+"@"+dbServer+"/"+dbFileName)
    }
    

    Now in your main package:

    func init() {
        utils.InitFirebird()
    }
    
    func main() {
        if beego.BConfig.RunMode == "dev" {
            beego.BConfig.WebConfig.DirectoryIndex = true
            beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
            }
    
        var n int
        defer utils.Conn.Close() // <-- Close here
    
        utils.Conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
        fmt.Println("Relations count=", n)
    
        beego.Run()
    }
    

    Here Close() won't get executed immediately because beego.Run() will block.

    PS: Passing DB connection using global variable is not recommended. If you want to learn more check this out: https://www.alexedwards.net/blog/organising-database-access

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题