Skip to content

Instantly share code, notes, and snippets.

@vedashree29296
Created February 9, 2021 06:18
Show Gist options
  • Save vedashree29296/2d4ad9a83d1c51503907138fc99510de to your computer and use it in GitHub Desktop.
Save vedashree29296/2d4ad9a83d1c51503907138fc99510de to your computer and use it in GitHub Desktop.
Get all categories
//GetAllCategory : Get all Categories from DB
func (c Category) GetAllCategory() ([]*models.Category, error) {
var categories []*models.Category // this will hold final result
//execute the query
rows, err := client.DbClient.Query("SELECT * from category")
defer rows.Close()
// iterate through rows
for rows.Next() {
// Bind to model
err = rows.Scan(&c.ID, &c.CategoryName)
// type cast to models.Category
cm := models.Category(c)
//add to result
categories = append(categories, &cm)
if err != nil {
return nil, err
}
}
// handle any errors that are encountered during iteration
err = rows.Err()
if err != nil {
return nil, err
}
return categories, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment