0

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

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() 函数中,我被困在这里。

如何返回所有表的详细信息?

请帮忙

4

1 回答 1

0

我得到了答案

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

}
于 2021-04-22T04:09:08.217 回答