go - Why is my tiedot DB pointer nil? -
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() { _, name := range mydb.allcols() { fmt.printf("i have collection called %s\n", name) } } func main() { idb() }
i next error:
runtime error: invalid memory address or nil pointer dereference
it because mydb nil, why , how can prepare can setup mydb in init?
note if drop in main without using global variable, works.
short variable declarations
a short variable declaration uses syntax:
shortvardecl = identifierlist ":=" expressionlist . it shorthand regular variable declaration initializer expressions no types:
"var" identifierlist = expressionlist . mydb local init function variable. := short variable declaration.
mydb, err := db.opendb("db") to update bundle mydb variable, write,
var err error mydb, err = db.opendb("db") go tiedot
No comments:
Post a Comment