Hi I am learning to use golang right now, and there is an api end point, where I want to return all the existing users in the database, however my query is returning only last user.
base.go < responsible for establishing db conns
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite")
var db *gorm.DB //database
func GetDB() *gorm.DB {
return db
}
models.go < responsible for data abstractions
type Account struct {
gorm.Model
Email string `json:"email"`
Password string `json:"password"`
Token string `json:"token";sql:"-"`
}
func GetAllUsers() *Account {
acc := &Account{}
rows, err := GetDB().Raw("select * from accounts").Rows()
if err != nil {
fmt.Print("error")
}
for rows.Next() {
GetDB().ScanRows(rows, &acc)
}
return acc
}