1

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

11
  • 3
    "Now I think I can do above logic" No you can not. func(params profile.GetProfileParams, principal *models.User) middleware.Responder is not the same as func(GetProfileParams, interface{}) middleware.Responder. That is: *models.User is not the same type as interface{}. Commented Jan 14, 2019 at 8:02
  • 2
    It doesn't matter what interface the GetProfileHandlerFunc type implements, the error is not saying anything about interfaces, it tells you that you cannot convert A to B, where A is the func literal func(params profile.GetProfileParams, principal *models.User) middleware.Responder and B is the func type GetProfileHandlerFunc. Commented Jan 14, 2019 at 9:21
  • 1
    ... It's the same as trying to do int("hello world"), you are doing conversion between two incompatible types. Commented Jan 14, 2019 at 9:24
  • 3
    @Himanshu. NO, you cannot have seen this because it is impossible in Go. See also golang.org/doc/faq#convert_slice_of_interface. Go's type system does not work the way you think. interface{} does not mean "whatever" it means interface{} and only interface{}. You can assign anything to interface{} but as a type it is interface{} and nothing else. Commented Jan 14, 2019 at 9:34
  • 1
    You've probably misunderstood what you've seen or the code was as well broken and would not compile just like yours. Commented Jan 14, 2019 at 9:35

1 Answer 1

1

the point is: if you have a function like

func A(param interface{}) {}

you can pass anything to param when you make a call to function A. like

A(10)
A(true)
A(nil)

Because interface{} means everything. So your handle func definition:

type GetProfileHandlerFunc func(GetProfileParams, interface{}) middleware.Responder

means a function GetProfileHandlerFunc which takes two params, the first is of type GetProfileParams, the second is of type interface{}. That means second param canbe anything.

But

func(params profile.GetProfileParams, principal *models.User) middleware.Responder

means a function which takes two params, the first is of type GetProfileParams, the second is of type *models.User. So, do you think they are the same? No.

I need a function can take anything as second param,not a function who can only take User as second function.

When you call your handle

GetProfileHandlerFunc(params, 10) // this is ok

So is that ok for

func(params profile.GetProfileParams, principal *models.User) middleware.Responder

No.

The right way to do this is :

api.ProfileGetProfileHandler = profile.GetProfileHandlerFunc(func(params profile.GetProfileParams, principal interface) middleware.Responder {

     user:=principal.(*model.User) // watch this.
}
Sign up to request clarification or add additional context in comments.

Comments

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.