全局变量不传递给下一个包

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 :)

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")
}