I am trying to fetch full table details using go language , but not getting
func GetallUserHandler(c *gin.Context) {
// fetch data
userdata, selectErr := views.GetallUser()
if selectErr != nil {
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Something wrong on our server"))
database.Dberror(selectErr)
} else {
c.JSON(http.StatusOK, userdata)
}
}
func GetallUser() error {
db := database.DbConnection()
defer db.Close()
query := "Select * from user;"
rows, queryErr := db.Query(query)
database.Dberror(queryErr)
defer rows.Close()
for rows.Next() {
user := models.User{}
queryErr = rows.Scan(&user.Id, &user.Username, &user.Password, &user.Firstname)
}
queryErr = rows.Err()
database.Dberror(queryErr)
}
in GetallUser() function , I am stuck here .
How can I return the all table details?
please help
I got the Answer
func GetallUser() ([]models.User, error) {
db := database.DbConnection()
defer db.Close()
query := "Select * from user;"
rows, queryErr := db.Query(query)
database.Dberror(queryErr)
defer rows.Close()
var userrslt []models.User
for rows.Next() {
user := models.User{}
rows.Scan(&user.Id, &user.Username, &user.Password, &user.Firstname)
userrslt = append(userrslt, user)
}
queryErr = rows.Err()
database.Dberror(queryErr)
return userrslt, queryErr
}