为什么我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 关注
- 161 浏览
添加回答
举报
0/150
提交
取消