英文:
Storing database connection in a global variable
问题
我正在使用提供的Golang的MySQL驱动程序。
https://github.com/go-sql-driver/mysql
我尝试做的一件事是将数据库变量存储在全局连接中。根据文档,sql.Open()应该返回一个指向DB结构体的指针,所以我尝试将其存储为
var db *DB
然而,这导致了错误
undefined: DB
接下来,我尝试查看MySQL驱动程序的源代码,在这里找到了一段代码 https://github.com/go-sql-driver/mysql/blob/master/driver.go
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
因此,我尝试将变量保存为driver.Conn - 但是,我无法导入(导入错误)。我也无法导入driver。
我最后尝试使用反射来揭示变量的名称
package main
import (
"fmt"
"reflect"
)
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
func main() {
db, _ := sql.Open("mysql", "root:password@/Tracker")
yt := reflect.TypeOf(db).Kind()
fmt.Printf("%T: %s\n", yt, yt)
}
不幸的是,这也不起作用 - 它显示为指针,而不是它实际指向的变量类型。
我现在不知道如何解决了。提前感谢您的帮助!
英文:
I'm using the MySQL driver for Golang provided here
https://github.com/go-sql-driver/mysql
One of the things I'm trying to do is store the database variable in a global connection. According to the documentation, sql.Open() is supposed to return a pointer to a DB struct, so I tried storing it as
var db *DB
However, that resulted in the error
undefined: DB
The next thing I tried was to look at the source code for the MySQL driver, and I found a snippet of code here https://github.com/go-sql-driver/mysql/blob/master/driver.go
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
So, I tried to save the variable as driver.Conn - however, I was unable to (incorrect imports). I wasn't able to import driver either.
The last thing I tried was to use reflect to bring the name of the variable to light
package main
import (
"fmt"
"reflect"
)
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
func main() {
db, _ := sql.Open("mysql", "root:password@/Tracker")
yt := reflect.TypeOf(db).Kind()
fmt.Printf("%T: %s\n", yt, yt)
}
Unfortunately, that didn't work either - it shows up as pointer, and not the type of variable it's actually pointing to.
I'm at a loss as to how to figure it out now. Thanks in advance for your help!
答案1
得分: 40
你需要使用包名来限定类型的名称:
import(
"database/sql"
"github.com/go-sql-driver/mysql"
)
var db *sql.DB // 注意sql包提供了命名空间
func main() {
var err error
// 确保不要遮蔽全局变量 - 只需使用 = 进行赋值 - 不要初始化一个新变量并使用 := 进行赋值
db, err = sql.Open(...)
if err != nil {
// 处理错误!
}
}
无论你是想要一个全局变量(容易入门)还是显式地传递它,都取决于你,但我建议现在保持简单。如果这将用于Web应用程序,你可以安全地在处理程序/请求之间共享*sql.DB
连接池。
英文:
You need to qualify the name of the type with the package name:
import(
"database/sql"
"github.com/go-sql-driver/mysql"
)
var db *sql.DB // Note the sql package provides the namespace
func main() {
var err error
// Make sure not to shadow your global - just assign with = - don't initialise a new variable and assign with :=
db, err = sql.Open(...)
if err != nil {
// Handle the error!
}
}
Whether you want to have a global (easy to get started) or pass it around explicitly is up to you, but I'd suggest just keeping it simple for now. If this is going into a web application, you can safely share the *sql.DB
connection pool across handlers/requests.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论