為什么我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
}
這是永遠不會發生的。
請參閱此示例。
- 1 回答
- 0 關注
- 201 瀏覽
添加回答
舉報
0/150
提交
取消