gpt4 book ai didi

go - 为什么 defer stmnt.Close() 似乎会阻止我的 http.Redirect?

转载 作者:IT王子 更新时间:2023-10-29 02:33:54 26 4
gpt4 key购买 nike

为什么我的 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)

}

最佳答案

使用 db.Exec 而不是 db.Query

Exec executes a query without returning any rows.

对比

Query executes a query that returns rows

至于为什么,我猜是mysql Rows.Close 正在等待数据on the connection :

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
}

这永远不会发生。

参见 this例如。

关于go - 为什么 defer stmnt.Close() 似乎会阻止我的 http.Redirect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32387502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com