Hello, You shadow your DB global variable in the DB, err := gorm.Open() call. Do something like this instead: var err error DB, err = gorm.Open()
Le lundi 14 novembre 2016 09:54:40 UTC+1, Rayland a écrit : > > Greetings fellow gophers, > > I have a library in my vendor folder that looks like this: > > package gorm > > import ( > "fmt" > "github.com/jinzhu/gorm" > _ "github.com/jinzhu/gorm/dialects/mysql" > conf "github.com/spf13/viper" > "math/rand" > "time" > ) > > var DB *gorm.DB > > func GetDB() (*gorm.DB, error) { > if DB != nil { > return DB, nil > } > > DB_NAME := conf.GetString("db.sql.mysql.dbname") > DB_USERNAME := conf.GetString("db.sql.mysql.username") > DB_PASSWORD := conf.GetString("db.sql.mysql.password") > DB_IPS := conf.GetStringSlice("db.sql.mysql.ips") > ipsNr := len(DB_IPS) > > rand.Seed(int64(time.Now().Nanosecond())) > > connString := > fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=latin1&parseTime=True", > DB_USERNAME, > DB_PASSWORD, > DB_IPS[rand.Intn(ipsNr)], > DB_NAME, > ) > > DB, err := gorm.Open("mysql", connString) > if err != nil { > return nil, err > } > > return DB, nil > } > > > And every time I use GetDB() I get a new database connection instead of > getting the one already initialized. > > When I do the same thing outside the vendor folder it works as intended. > > What's the catch here? > > > Thank you > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.