在init时
var ( DB gorm.DB ) func init() { DB, DbErr := gorm.Open("sqlite3", "./db/db.sqlite3") if DbErr != nil { log.Fatal("error daabase") } }
出现
PANIC: runtime error: invalid memory address or nil pointer dereference
请这样
var ( DB gorm.DB DbErr error ) func init() { DB, DbErr = gorm.Open("sqlite3", "./db/db.sqlite3") if DbErr != nil { log.Fatal("error daabase") } }