1

I have this function:

func functionX(collection []*interface{}) {
    ...
    response, err := json.MarshalIndent(collection, "", "  ")
    ...
}

I want the collection parameter to allow arrays of any kind, that's why I tried with *interface{} but I'm receiving errors like this:

cannot use MyDataType (type []*model.MyDataType) as type []*interface {} in argument to middleware.functionX
Oriam
  • 641
  • 10
  • 19
  • 1
    possible duplicate of [Cannot convert \[\]string to \[\]interface {}](http://stackoverflow.com/questions/12990338/cannot-convert-string-to-interface) – Ainar-G Aug 20 '15 at 20:59

1 Answers1

5

You can't do it that way, however you can easily do this:

func functionX(collection interface{}) error {
    ...
    response, err := json.MarshalIndent(collection, "", "  ")
    ...
}

playground

OneOfOne
  • 95,033
  • 20
  • 184
  • 185
  • Thanks, I already figured it out, but is exactly as you say :) It's working like a charm – Oriam Aug 20 '15 at 21:14