如何在 Go 中将服务指针从一个包传递到另一个包?
How to pass service pointers from one package to another package in Go?
我根据它们与哪个页面相关,将不同的 http.HandleFunc
分隔在不同的文件中。我使用 gorilla/sessions
来管理客户端会话和用户身份验证,我使用 go-sql-driver
来访问 MySQL 数据库。
项目布局:
<PROJECT ROOT>
-> <cliend> // This folder containing html, css, and js
-> <pagehndler>
-> <index>
-> index.go // Need to access database and session
-> <projbrwsr>
-> projbrwsr.go // Need to access database and session
-> main.go
因此,我有 1 个指针指向 go-sql-driver
服务
db, err := sql.Open("mysql", "user:password@/dbname")
和 1 个指向 gorilla/sessions
服务的指针
var store = sessions.NewCookieStore([]byte("something-very-secret"))
在我的理解中有两种方法可以将两个指针传递给其他包:
将两个指针包装成两个包(sess
,db
)并导出。并且,需要导入包所需服务的包 (sess
、db
)。并调用导出的指针。
<PROJECT ROOT>
-> <cliend> // This folder containing html, css, and js
-> <pagehndler>
-> <index>
-> index.go // Need to access database and session
-> <projbrwsr>
-> projbrwsr.go // Need to access database and session
-> <service>
-> sess.go // Storing the database service pointer
-> db.go // Storing the session service pointer
-> main.go
初始化主包中的两个指针并将它们作为参数传递给包含页面句柄函数的另一个包。在另一个包中,将 args 设置为局部变量,以便我们可以在另一个包中本地调用它。
<PROJECT ROOT>
-> <cliend> // This folder containing html, css, and js
-> <pagehndler>
-> <index>
-> index.go // Need to access database and session
// Containing a func newService(db *DB)
-> <projbrwsr>
-> projbrwsr.go // Need to access database and session
// Containing a func newService(sess *CookieStore)
-> main.go
将这两个指针传递给其他包以供其他句柄函数调用的最佳方法是什么?
这是应该起作用的概念。
您应该在包内声明一个导出的变量。这个变量必须是一个指针。
var myVar = 3
var MyPointer = &myVar
https://play.golang.org/p/EQDwGF7pjv
从您的主包中,您可以将指针设置为您的 "global" 数据库或会话地址。
mypackage.MyPointer = dbPointer
然后你的包里面的指针指向你的dbPointer。我认为这是将指针传递给其他包的好方法。
我根据它们与哪个页面相关,将不同的 http.HandleFunc
分隔在不同的文件中。我使用 gorilla/sessions
来管理客户端会话和用户身份验证,我使用 go-sql-driver
来访问 MySQL 数据库。
项目布局:
<PROJECT ROOT>
-> <cliend> // This folder containing html, css, and js
-> <pagehndler>
-> <index>
-> index.go // Need to access database and session
-> <projbrwsr>
-> projbrwsr.go // Need to access database and session
-> main.go
因此,我有 1 个指针指向 go-sql-driver
服务
db, err := sql.Open("mysql", "user:password@/dbname")
和 1 个指向 gorilla/sessions
服务的指针
var store = sessions.NewCookieStore([]byte("something-very-secret"))
在我的理解中有两种方法可以将两个指针传递给其他包:
将两个指针包装成两个包(
sess
,db
)并导出。并且,需要导入包所需服务的包 (sess
、db
)。并调用导出的指针。<PROJECT ROOT> -> <cliend> // This folder containing html, css, and js -> <pagehndler> -> <index> -> index.go // Need to access database and session -> <projbrwsr> -> projbrwsr.go // Need to access database and session -> <service> -> sess.go // Storing the database service pointer -> db.go // Storing the session service pointer -> main.go
初始化主包中的两个指针并将它们作为参数传递给包含页面句柄函数的另一个包。在另一个包中,将 args 设置为局部变量,以便我们可以在另一个包中本地调用它。
<PROJECT ROOT> -> <cliend> // This folder containing html, css, and js -> <pagehndler> -> <index> -> index.go // Need to access database and session // Containing a func newService(db *DB) -> <projbrwsr> -> projbrwsr.go // Need to access database and session // Containing a func newService(sess *CookieStore) -> main.go
将这两个指针传递给其他包以供其他句柄函数调用的最佳方法是什么?
这是应该起作用的概念。
您应该在包内声明一个导出的变量。这个变量必须是一个指针。
var myVar = 3
var MyPointer = &myVar
https://play.golang.org/p/EQDwGF7pjv
从您的主包中,您可以将指针设置为您的 "global" 数据库或会话地址。
mypackage.MyPointer = dbPointer
然后你的包里面的指针指向你的dbPointer。我认为这是将指针传递给其他包的好方法。