I have created few types including interface as:
// GetProfileHandlerFunc turns a function with the right signature into a get profile handler
type GetProfileHandlerFunc func(GetProfileParams, interface{}) middleware.Responder
// Handle executing the request and returning a response
func (fn GetProfileHandlerFunc) Handle(params GetProfileParams, principal interface{}) middleware.Responder {
return fn(params, principal)
}
// GetProfileHandler interface for that can handle valid get profile params
type GetProfileHandler interface {
Handle(GetProfileParams, interface{}) middleware.Responder
}
Now in my api implementation package. I am using a logic to handle the request parameters. I am trying to assign GetProfileHandlerFunc to another type since it implements GetProfileHandler interface as you can see above.
api.ProfileGetProfileHandler = profile.GetProfileHandlerFunc(func(params profile.GetProfileParams, principal *models.User) middleware.Responder {
// contains logic to handle the request
}
Now I think I can do above logic. But I am getting type mismatch error.
cannot convert func literal (type func(profile.GetProfileParams, *"userproj/models".User) middleware.Responder) to type profile.GetProfileHandlerFuncgo
func(params profile.GetProfileParams, principal *models.User) middleware.Responderis not the same asfunc(GetProfileParams, interface{}) middleware.Responder. That is:*models.Useris not the same type asinterface{}.GetProfileHandlerFunctype implements, the error is not saying anything about interfaces, it tells you that you cannot convert A to B, where A is the func literalfunc(params profile.GetProfileParams, principal *models.User) middleware.Responderand B is the func typeGetProfileHandlerFunc.int("hello world"), you are doing conversion between two incompatible types.interface{}does not mean "whatever" it meansinterface{}and onlyinterface{}. You can assign anything tointerface{}but as a type it isinterface{}and nothing else.