select 在go语言中使用查询- MYSQL

select query using in go language- MYSQL

我正在尝试使用 go 语言获取完整的 table 详细信息,但没有获取

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)

}

在 GetallUser() 函数中,我卡在这里了。

如何 return 所有 table 详细信息?

请帮忙

我得到了答案

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

}