GO函数参数中的通用类型数组

Ori*_*iam 1 go

我有这个功能:

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

我希望collection参数允许任何类型的数组,这就是为什么我尝试使用*interface {}但我收到的错误是这样的:

cannot use MyDataType (type []*model.MyDataType) as type []*interface {} in argument to middleware.functionX
Run Code Online (Sandbox Code Playgroud)

One*_*One 5

你无法这样做,但是你可以轻松地做到这一点:

func functionX(collection interface{}) error {
    ...
    response, err := json.MarshalIndent(collection, "", "  ")
    ...
}
Run Code Online (Sandbox Code Playgroud)

playground