英文:
Global Variable Not Passing To Next Package
问题
由于golang不支持循环依赖,并且我刚刚开始使用golang,我在连接变量方面遇到了一些问题。我正在使用gorm驱动程序和MySQL作为我的数据库引擎。
我试图将连接变量传递给程序调用的下一个函数,该函数位于另一个文件中。
AchFactory.go
fmt.Println(">Loading Achievements")
Start.DB.HasTable("achievements")
Start.DB.Select("id", "group_name", "category", "level", "reward_pixels", "reward_points", "progress_needed", "game_id").Find("achievements")
fmt.Println(">Found Schema")
Start.go
var DB *gorm.DB
func Initi() {
fmt.Println("Initating!")
Connect()
}
func Connect() {
var err error
DB, err = gorm.Open("mysql", config.MYSQL_USER+":"+config.MYSQL_PASS+"@"+"/"+config.MYSQL_DB+"?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic(err)
DB.Close()
}
fmt.Println(">Connection Succesful")
fmt.Println(">Test Query Starting")
TestQ()
}
func TestQ() {
DB.HasTable("tests")
fmt.Println(">Query Succesful!")
AchManager.GetAch()
}
我只想知道我做错了什么,以便将来可以避免这个错误。谢谢
英文:
As golang does not support circular dependencies, and I am only just starting in golang. I've run into a little wall with my connection variable, I am using the gorm driver, and MySQL as my database engine.
And I'm trying to pass on the connection variable to the next function my program calls, which is in another file.
AchFactory.go
fmt.Println(">Loading Achievements")
Start.DB.HasTable("achievements")
Start.DB.Select("id", "group_name", "category", "level", "reward_pixels", "reward_points", "progress_needed", "game_id").Find("achievements")
fmt.Println(">Found Schema")
Start.go
var DB *gorm.DB
func Initi() {
fmt.Println("Initating!")
Connect()
}
func Connect() {
var err error
DB, err = gorm.Open("mysql", config.MYSQL_USER+":"+config.MYSQL_PASS+"@/"+config.MYSQL_DB+"?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic(err)
DB.Close()
}
fmt.Println(">Connection Succesful")
fmt.Println(">Test Query Starting")
TestQ()
}
func TestQ() {
DB.HasTable("tests")
fmt.Println(">Query Succesful!")
AchManager.GetAch()
}
Would just like to know what i've done wrong so i can prevent this error in the future. Thank you
答案1
得分: 1
如果你的AchFactory.go
文件和Start.go
文件在同一个包中,你就不需要在DB
前面加任何前缀,因为这两个文件都在同一个包中,它们共享在包级别定义的变量。
例如:
.
└── foo
├── AchFactory.go
└── Start.go
Start.go
package foo
// ...
var DB *gorm.DB
// ...
AchFactory.go
package foo
func F() {
fmt.Println("Loading Achievements")
DB.HasTable("achievements")
DB.Select("id", "group_name", "category", "level", "reward_pixels", "reward_points", "progress_needed", "game_id").Find("achievements")
fmt.Println("Found Schema")
}
另一方面,如果AchFactory.go
和Start.go
位于不同的包中,那么AchFactory.go
文件需要导入包含Start.go
文件的包,并且要访问导入包的公开成员,你需要用包的名称作为前缀,并用点号连接两者;例如packagename.Membername
。
例如:
.
├── foo
│ └── Start.go
└── bar
└── AchFactory.go
Start.go
package foo
// ...
var DB *gorm.DB
// ...
AchFactory.go
package bar
import (
"fmt"
"path/to/foo"
)
func F() {
fmt.Println("Loading Achievements")
foo.DB.HasTable("achievements")
foo.DB.Select("id", "group_name", "category", "level", "reward_pixels", "reward_points", "progress_needed", "game_id").Find("achievements")
fmt.Println("Found Schema")
}
英文:
If your AchFactory.go
file is in the same package as your Start.go
file you don't need to prefix DB
with anything, since both files live in the same package they both share the variables defined at the package level.
For example:
.
└── foo
├── AchFactory.go
└── Start.go
Start.go
package foo
// ...
var DB *gorm.DB
// ...
AchFactory.go
package foo
func F() {
fmt.Println(">Loading Achievements")
DB.HasTable("achievements")
DB.Select("id", "group_name", "category", "level", "reward_pixels", "reward_points", "progress_needed", "game_id").Find("achievements")
fmt.Println(">Found Schema")
}
If, on the other hand, AchFactory.go
lives in a different package than Start.go
then the AchFactory.go
file needs to import
the package that contains the Start.go
file, and then to access the exported members of the imported pacakge you prefix them with the name of the package and join the two with a dot; e.g. packagename.Membername
.
For example:
.
├── foo
│   └── Start.go
└── bar
└── AchFactory.go
Start.go
package foo
// ...
var DB *gorm.DB
// ...
AchFactory.go
package bar
import (
"fmt"
"path/to/foo"
)
func F() {
fmt.Println(">Loading Achievements")
foo.DB.HasTable("achievements")
foo.DB.Select("id", "group_name", "category", "level", "reward_pixels", "reward_points", "progress_needed", "game_id").Find("achievements")
fmt.Println(">Found Schema")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论