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?