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

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

無法使用 Golang 從帶有 mySQL 后端的大猩猩/會話中獲取價值

無法使用 Golang 從帶有 mySQL 后端的大猩猩/會話中獲取價值

Go
嗶嗶one 2023-06-05 19:34:51
我試圖在帶有 mySQL 后端的大猩猩會話中為我的模型保存一個結構,但當我嘗試檢索它時,venueID 只得到 0。我可以毫不費力地保存和獲取即顯消息。我的目標是在會話中保存模型結構并檢索它以獲取編輯、更新和刪除功能中的 ID 號。這是我的代碼:type appResource struct {    tmpl  *template.Template // net/http    store *mysqlstore.MySQLStore    db    *sql.DB // database/sql}// newAppResource function to pass global varfunc newAppResource(store *mysqlstore.MySQLStore, db *sql.DB, tmpl *template.Template) *appResource {    return &appResource{        store: store,        db:    db,        tmpl:  tmpl,    }}func main() {sessionKey := os.Getenv("sessionKey")    endpoint := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName)    tableName := "sessions"    path := "/"    maxAge := 3600    codecs := []byte(sessionKey)    store, err := mysqlstore.NewMySQLStore(endpoint, tableName, path, maxAge, codecs)    if err != nil {        log.Println("SESSIONS STORE error")        log.Fatal(err)....    }}type Venue struct {    VenueID int    Name    string    Email   string    Phone   string    Active  bool}// VenueData template variable for show and edittype VenueData struct {    Venue   Venue    Flashes []interface{}}func (rs *appResource) venuesShow(w http.ResponseWriter, r *http.Request) {    var venue Venue    var data VenueData    id := r.URL.Query().Get("id")    venueID, err := strconv.Atoi(id)    if err != nil {        log.Println("show venue ID not > 0")        http.Redirect(w, r, "/login", http.StatusUnauthorized)        return    }    if !(venueID > 0) {        log.Println("update venue ID not > 0")        http.Redirect(w, r, "/login", http.StatusUnauthorized)        return    }    query, err := rs.db.Query("SELECT id, name, email, phone, active FROM Venues WHERE id=?", venueID)    if err != nil {        log.Fatal(err)        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

事實證明,我需要注冊一個結構才能在 Gorilla 會話中使用它。


import (

"encoding/gob"

)


func main() {

 gob.Register(Venue{})

}


查看完整回答
反對 回復 2023-06-05
  • 1 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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