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

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

為什么 defer stmnt.Close() 似乎阻止了我的 http.Redirect?

為什么 defer stmnt.Close() 似乎阻止了我的 http.Redirect?

Go
達令說 2021-11-08 09:59:21
為什么我defer stmnt.Close()似乎阻止我http.Redirect重定向它只是掛在網站上無限嘗試加載。但是如果我刪除defer stmnt.Close()它重定向就好了?    err = db.QueryRow("SELECT steamid FROM accounts WHERE steamid = ?", ids).Scan(&steamid)    if err != nil {        common.WriteLog(err.Error(), r)        http.Error(w, "Failed to connect to database. Try again in a bit.", 500)    }    switch {    case len(profile.Response.Players) == 0:        common.WriteLog("Failed to look you up in the steam database. Try again in a bit.", r)        http.Error(w, "Failed to look you up in the steam database. Try again in a bit.", 500)    case err == sql.ErrNoRows:        stmnt, err := db.Query("INSERT INTO accounts SET steamid=?", ids)        if err != nil {            common.WriteLog(err.Error(), r)            http.Error(w, "Failed to insert your account to the database. Try again in a bit.", 500)        }        defer stmnt.Close() // <<<<< The suspect        // Insert Account        http.Redirect(w, r, "/", 303)    case err != nil:        common.WriteLog(err.Error(), r)        http.Error(w, "Failed to insert your account to the database. Try again in a bit.", 500)    default:        // Login User        http.Redirect(w, r, "/", 303)    }
查看完整描述

1 回答

?
海綿寶寶撒

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

使用db.Exec代替db.Query。


Exec 執行查詢而不返回任何行。


對比


Query 執行返回行的查詢


至于為什么,我猜mysqlRows.Close正在等待連接上的數據:


func (rows *mysqlRows) Close() error {

    mc := rows.mc

    if mc == nil {

        return nil

    }

    if mc.netConn == nil {

        return ErrInvalidConn

    }


    // Remove unread packets from stream

    err := mc.readUntilEOF()

    rows.mc = nil

    return err

}

這是永遠不會發生的。


請參閱示例。


查看完整回答
反對 回復 2021-11-08
  • 1 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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