So am learning Go - Language atm and I still confused on how do I iterate arraylist that I get from SQL when I execute my query
here is for the detail
I got a file named CustomerDao.go which contains a bunch of queries and the query I uses now is
SELECT mst_customer.mcus_mlok_pk , mst_customer.mcus_mlok_kode , mst_customer.mcus_nama FROM frontend.mst_customer;
which will return 50 rows of data and 3 columns yes? now the confusing part is when I reach my controller which is like this
func GetArrayListCustomer(queryType string) (map[string]CustomerModelResponse, error) {
logger := customlogger.GetInstance()
logger.Println("Service: GetArrayListCustomer Start")
queryText := dao.CustomerDAO(queryType)
res, err := config.GetDbByPath("myDatabase").GetDb().Query(queryText)
mapList := make(map[string]CustomerModelResponse)
var custResponse CustomerModelResponse
if config.FancyHandleError(err) {
logger.Println("Service: GetArrayListCustomer -> Error " + err.Error())
return mapList, err
}
defer res.Close()
for res.Next() {
errs := res.Scan(&custResponse.McusmlokPk, &custResponse.McusmlokKode, &custResponse.McusNama)
for _, eachCust := range res {
//the error goes here , for some reason
}
}
return mapList, nil
}
and then I try to loop the value then the error show that I cannot range over res which is a *sql.Rows how do I declare those result as an array not as a *sql.Rows type?
You need to scan your values into a struct, using rows.Next
you loop over the sql results your query gives, you need to scan those results (on each loop) into your desired struct, and save that struct into something (a map in this case)
func GetArrayListCustomer(queryType string) (map[string]CustomerModelResponse, error) {
logger := customlogger.GetInstance()
logger.Println("Service: GetArrayListCustomer Start")
queryText := dao.CustomerDAO(queryType)
res, err := config.GetDbByPath("myDatabase").GetDb().Query(queryText)
mapList := make(map[string]CustomerModelResponse)
if config.FancyHandleError(err) {
logger.Println("Service: GetArrayListCustomer -> Error " + err.Error())
return mapList, err
}
defer res.Close()
for res.Next() {
var custResponse CustomerModelResponse
if err := res.Scan(&custResponse.McusmlokPk, &custResponse.McusmlokKode, &custResponse.McusNama); err != nil {
// Handle error
}
mapList[cutResponse.McusmlokPk] = custResponse
}
// Now mapList is a map with McusmlokPk as key and CustomerModelResponse struct as value
return mapList, nil
}
You can not use for range
loop on res
. res
can only be iterated using Next()
. Following is what you can do:
for res.Next() {
//CREATE A NEW ZERO VALUE CustomerModelResponse FOR EACH ITERATION
var custResponse CustomerModelResponse
// UPDATE CustomerModelResponse WITH ROW DATA
errs := res.Scan(&custResponse.McusmlokPk, &custResponse.McusmlokKode, &custResponse.McusNama)
if errs == nil {
// APPEND THE NEWLY CREATED & UPDATED CustomerModelResponse TO mapList
// ASSUMINg mst_customer.mcus_mlok_pk AS KEY FOR THE MAP
mapList[custResponse.McusmlokPk] = custResponse
}
/*
THIS LOOP IS NOT NEEDED ANY MORE
for _, eachCust := range res {
//the error goes here , for some reason
}
*/
}
I hope this helps.