通常我寫這樣的事情更好/更短的方式來關閉圍棋
rows := db.MyPgConn.QueryRows(`SELECT * FROM bla`) // or any other query
for rows.Next() { // if there are result
// rows.Scan(
// do custom operation
}
rows.Close() // close recordset
但這樣一來,有可能是我忘了寫rows.Close()
就這樣code,可以使可用的數量連接/套接字耗盡,有沒有更好的方法來做到這一點?
來自https://golang.org/doc/effective_go.html#defer「Go的延遲語句計劃了一個函數調用(延遲函數),在執行延遲的函數返回之前立即運行。有效的方法來處理諸如必須釋放的資源等情況,而不管函數返回的路徑。「 – Intermernet 2015-01-21 04:57:40