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
1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I already figured it out, but is exactly as you say :) It's working like a charm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.