dongxianshuai8927 2017-05-12 04:09
浏览 25

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

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条回答 默认 最新

  • douxuanjie2692 2017-05-12 08:13
    关注

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

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大