【发布时间】:2021-09-21 20:18:17
【问题描述】:
我正在尝试运行程序this link。但是我运行它并导致gorm 方面的恐慌。由于我是 go 语言的新手,我不知道如何调试它。
程序的迷你版(没有fb、twitter等登录界面)
package main
import (
"net/http"
"github.com/qor/auth"
"github.com/qor/auth/auth_identity"
"github.com/qor/auth/providers/password"
"github.com/qor/session/manager"
"github.com/jinzhu/gorm"
)
var (
gormDB, _ = gorm.Open("sqlite3", "sample.db")
Auth = auth.New(&auth.Config{
DB: gormDB,
})
)
func init() {
// Migrate AuthIdentity model, AuthIdentity will be used to save auth info, like username/password, oauth token, you could change that.
gormDB.AutoMigrate(&auth_identity.AuthIdentity{})
// Register Auth providers
// Allow use username/password
Auth.RegisterProvider(password.New(&password.Config{}))
}
func main() {
mux := http.NewServeMux()
// Mount Auth to Router
mux.Handle("/auth/", Auth.NewServeMux())
http.ListenAndServe(":9000", manager.SessionManager.Middleware(mux))
}
我将我命名为main.go 的文件放在一个文件夹中(main.go 是该文件夹中唯一的文件),然后我运行go mod init project_name && go mod tidy 来初始化项目并安装所需的包。然后我执行go run . 并得到以下信息:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x6d0441]
goroutine 1 [running]:
database/sql.(*DB).conn(0x0, 0x953c10, 0xc000016150, 0x1, 0x7f9cd7c2b108, 0x18, 0xc00028dac8)
/snap/go/7736/src/database/sql/sql.go:1197 +0x41
database/sql.(*DB).query(0x0, 0x953c10, 0xc000016150, 0x8db986, 0x40, 0xc00039be10, 0x1, 0x1, 0xc000109b01, 0xc00028dba0, ...)
/snap/go/7736/src/database/sql/sql.go:1623 +0x66
database/sql.(*DB).QueryContext(0x0, 0x953c10, 0xc000016150, 0x8db986, 0x40, 0xc00039be10, 0x1, 0x1, 0xc00028dc10, 0x40cefb, ...)
/snap/go/7736/src/database/sql/sql.go:1605 +0xd4
database/sql.(*DB).QueryRowContext(...)
/snap/go/7736/src/database/sql/sql.go:1706
database/sql.(*DB).QueryRow(0x0, 0x8db986, 0x40, 0xc00039be10, 0x1, 0x1, 0xf)
/snap/go/7736/src/database/sql/sql.go:1717 +0x8b
github.com/jinzhu/gorm.sqlite3.HasTable(0x953d60, 0x0, 0xc00038ebf0, 0xc00038ebf0, 0xf, 0x11)
/home/username/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/dialect_sqlite3.go:81 +0xd8
github.com/jinzhu/gorm.(*Scope).autoMigrate(0xc000212e00, 0x85d340)
/home/username/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/scope.go:1268 +0xbb
github.com/jinzhu/gorm.(*DB).AutoMigrate(0xc00033de10, 0xc00028de18, 0x1, 0x1, 0xc00039b9c0)
/home/username/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/main.go:689 +0x97
main.init.0()
/home/username/tmp/qor-test/main.go:20 +0x7b
exit status 2
我真的迷路了,因为我不知道如何调试它。似乎是auth_identity.AuthIdentity struc 中的一个指针(我不知道如何更改)。顺便说一句,我正在使用go version go1.16.5 linux/amd64。
【问题讨论】: