I have searched for a solution on the net and in SO, but found nothing that apply to return values. It is a simple sql query with several rows that I want to return. Error handling not included:
func Fetch(query string) (string) {
type User struct{
id string
name string
}
rows, err := db.Query(query)
users := make([]*User, 0)
for rows.Next() {
user := new(User)
err := rows.Scan(&user.id, &user.name)
users = append(users, user)
}
return(users)
}
I get this error when compiling:
cannot use users (type *User) as type string in return argument
On a side note, you should also try to return every error and handle it where Fetch is being called.
Here is commonly how I would write your fetch function. (I probably wouldn’t personally pass the query in if I know what I’m already scanning but that’s something else).