1 回答

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
兩者一旦退出就會(huì)被關(guān)閉,在這兩個(gè)關(guān)閉后,您將無(wú)法再使用。connectionrowsQueryrows
解決這個(gè)問題的一種方法是傳遞一個(gè)閉包,并在關(guān)閉兩個(gè)資源之前執(zhí)行它:QueryQuery
func Query(query string, scan func(*sql.Rows) error) error {
connect, err := database.Connect()
if err != nil{
return err
}
defer connect.Close()
rows, err := connect.Query(query)
if err != nil{
return err
}
defer rows.Close()
return scan(rows)
}
func ProductList() ([]Hasil, error) {
var result []Hasil
err := model.Query("SELECT * FROM alamat_store", func(rows *sql.Rows) error {
for rows.Next() {
var each = Hasil{}
var err = rows.Scan(&each.Id_alamat_store, &each.Id_tk, &each.Alamat, &each.Id_wil, &each.Latitude, &each.Longitude)
if err != nil {
return err
}
result = append(result, each)
}
return nil
})
if err != nil {
return nil, err
}
return result, nil
}
- 1 回答
- 0 關(guān)注
- 96 瀏覽
添加回答
舉報(bào)