亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

QOR示例恐慌

QOR示例恐慌

Go
ABOUTYOU 2022-09-12 20:26:19
我正在嘗試運行該程序此鏈接。然而,我運行它,它導致側面的恐慌。由于我是語言新手,我不知道如何調試它。gormgo該程序的迷你版本(沒有fb,推特和其他登錄界面)package mainimport (    "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.gomain.gogo mod init project_name && go mod tidygo run .panic: runtime error: invalid memory address or nil pointer dereference[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x6d0441]我真的很迷茫,因為我不知道如何調試它。似乎是結構中的指針(我不知道如何更改)。順便說一句,我正在使用.auth_identity.AuthIdentitygo version go1.16.5 linux/amd64
查看完整描述

3 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

這似乎不是在戈爾姆中正確打開 SQLite 數據庫的方法。


您缺少 SQLite 驅動程序的導入,而不是傳遞字符串“sqlite3”,而應該傳遞和指向 .sqlite.Open("sample.db")gorm.Config


請參閱 https://gorm.io/docs/connecting_to_the_database.html#SQLite


import (

  "gorm.io/driver/sqlite"

  "gorm.io/gorm"

)


// github.com/mattn/go-sqlite3

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})


查看完整回答
反對 回復 2022-09-12
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

func init在建立數據庫連接之前執行,gorm 無法遷移,并且在此處引發恐慌。


試試這個代碼




func main(){

    gormDB, err = gorm.Open("sqlite3", "sample.db")

    if err != nil {

      log.Falal(err) // thrown, if database cannot be opened

    }

    // database connection is established, ready to perform migrations:



    Auth = auth.New(&auth.Config{

        DB: gormDB,

    })


    // Migrate AuthIdentity model, AuthIdentity will be used to save auth info, like username/password, oauth token, you could change that.

    err = gormDB.AutoMigrate(&auth_identity.AuthIdentity{})

    if err != nil {  

        log.Fatal(err) // do not forget to throw exception, if migration fails

    }


    // Register Auth providers

    // Allow use username/password

    Auth.RegisterProvider(password.New(&password.Config{}))



    err = gormDB.AutoMigrate(&auth_identity.AuthIdentity{})

    if err != nil {  

       log.Fatal(err) // do not forget to throw exception, if migration fails

    }

    // Register Auth providers

    // Allow use username/password

    Auth.RegisterProvider(password.New(&password.Config{}))


    mux := http.NewServeMux()


    // Mount Auth to Router

    mux.Handle("/auth/", Auth.NewServeMux())

    http.ListenAndServe(":9000", manager.SessionManager.Middleware(mux))


}


查看完整回答
反對 回復 2022-09-12
?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

問題是沒有開箱即用的支持。在教程中,他們忘記在導入中添加以下行:sqlite

_ "github.com/jinzhu/gorm/dialects/sqlite"


查看完整回答
反對 回復 2022-09-12
  • 3 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號