var myDB *db.DB
func init() {
myDB, err := db.OpenDB("db")
if err := myDB.Create("Feeds"); err != nil {}
if err := myDB.Create("Votes"); err != nil {}
}
func idb() {
for _, name := range myDB.AllCols() {
fmt.Printf("I have a collection called %s
", name)
}
}
func main() {
idb()
}
I get the following error:
runtime error: invalid memory address or nil pointer dereference
It is probably because myDB
is nil
, but why and how can I fix it so I can setup myDB in init?
Note that if I just drop everything in main without using a global variable, it works.