參考 astaxie 的代碼import (
_ "code.google.com/p/go-mysql-driver/mysql"
"database/sql"
"fmt"
//"time")
func main() {
db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8") checkErr(err) //插入數(shù)據(jù)
stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?") checkErr(err)
res, err := stmt.Exec("astaxie", "研發(fā)部門", "2012-12-09") checkErr(err)
id, err := res.LastInsertId() checkErr(err)
fmt.Println(id) //刪除數(shù)據(jù)
stmt, err = db.Prepare("delete from userinfo where uid=?") checkErr(err)
res, err = stmt.Exec(id) checkErr(err)
affect, err = res.RowsAffected() checkErr(err)
fmt.Println(affect)
}
func checkErr(err error) { if err != nil { panic(err)
}
}求教:這段代碼對(duì)數(shù)據(jù)庫(kù)的連接沒(méi)有主動(dòng)關(guān)閉(調(diào)用 db.Close()), 是不是Go的垃圾收集自動(dòng)關(guān)閉釋放資源呢? 還是必須手動(dòng)調(diào)用db.Close()釋放資源?
難道是必須手動(dòng)調(diào)用db.Close()釋放資源?
慕運(yùn)維8079593
2023-04-16 17:13:17