I have this func:
func Middleware(adapters ...interface{}) http.HandlerFunc {
// ...
}
and I am trying to call it via:
middleware := []mw.Adapter{mw.Error(),mw.Auth("x-huru-api-token")}
router.HandleFunc("/person_data_field",
mw.Middleware(middleware...,h.makeGetMany(v))).Methods("GET")
this doesn't compile though, I get:
In any case, I need to spread the slice so that it is sent as multiple arguments not just one arg...
With JS, it just looks like this:
const v = [1,2,3];
const f = function(...values){
console.log(values); // [1,2,3,4]
};
f(...v,4);
You can pass variadic arguments individually or as a single slice. You cannot mix and match. The slice element type must match the variadic argument type.
To fix the problem, put all of the variadic arguments in a slice of []interface{}
:
middleware := []interface{}{mw.Error(),mw.Auth("x-huru-api-token"), h.makeGetMany(v)}
router.HandleFunc("/person_data_field", mw.Middleware(middleware...)).Methods("GET")
Use slice tricks if you cannot build the slice directly as shown in the previous snippet.
middleware := []mw.Adapter{mw.Error(),mw.Auth("x-huru-api-token")}
router.HandleFunc("/person_data_field", mw.Middleware(
append(append([]interface{}{}, middleware...), h.makeGetMany(v)))).Methods("GET")