陶醉于mgo.v2-如何将集合中的所有数据作为json返回?

I'm trying to build an API endpoint using Revel for Go.

My models/models.go looks like this -

type Category struct {
Name string        `bson:"name"`
Slug  string        `bson:"slug"`}

func GetCategories(s *mgo.Session) *Category {
var results []Category
Collection(s).Find(nil).All(&results)
return results}

My controllers/book.go looks like this -

type Category struct {
*revel.Controller
revelbasic.MongoController}

func (c Category) Categories() revel.Result {
b := models.GetCategories(c.MongoSession)

return c.RenderJson(b)}

I've configured my conf/routes like this -

GET /categories Book.Categories

When I run the code, I get this error -

cannot use results (type []Category) as type *Category in return argument

I understand that I'm doing something wrong with the database query. Please help!

The error in your code is because of type mismatch between function GetCategories's return value declaration and what you are actually returning. To fix, change the return type to return a slice of results:

func GetCategories(s *mgo.Session) []Category {
    var results []Category
    Collection(s).Find(nil).All(&results)
    return results
}