title: Deserialize cursor into array with mongo-go-driver and interface
body: <p>I create an api using golang, i would like to create some functionnal test, for that i create an interface to abstract my database. But for that i need to be able to convert the cursor to an array without knowing the type.</p>
  
  <pre class="lang-golang prettyprint-override"><code>
  func (self *KeyController) GetKey(c echo.Context) (err error) {
      var res []dto.Key
      err = db.Keys.Find(bson.M{}, 10, 0, &amp;res)
      if err != nil {
          fmt.Println(err)
          return c.String(http.StatusInternalServerError, "internal error")
      }
      c.JSON(http.StatusOK, res)
      return
  }
  //THE FIND FUNCTION ON THE DB PACKAGE
  func (s MongoCollection) Find(filter bson.M, limit int, offset int, res interface{}) (err error) {
      ctx := context.Background()
      var cursor *mongo.Cursor
      l := int64(limit)
      o := int64(offset)
      objectType := reflect.TypeOf(res).Elem()
      cursor, err = s.c.Find(ctx, filter, &amp;options.FindOptions{
          Limit: &amp;l,
          Skip:  &amp;o,
      })
  
      if err != nil {
          return
      }
  
      defer cursor.Close(ctx)
  
      for cursor.Next(ctx) {
          result := reflect.New(objectType).Interface()
          err := cursor.Decode(&amp;result)
          if err != nil {
              panic(err)
          }
          res = append(res.([]interface{}), result)
  
      }
      return
  }
  
  
  </code></pre>
  
  <p>Does someone have an idea?</p>