-1

I want the behavior of a function to change based on the receiver. Or really, I want a method to be able to take in different receivers as input. For instance

type handler func(http.ResponseWriter, *http.Request, *Context)
type requireloggedinhandler handler

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  ctx := setupContext(...)
  // NEXT LINE IS THE KEY LINE
  if (reflect.TypeOf(h) == main.requireloggedinhandler) {
     if !checkedLoggedIn(ctx) {
         http.Redirect(...)
         return
     }
  }

  h(w, r, ctx)
}

But the issue is once we get to ServeHTTP the type has to be handler, not requireloggedinhandler. eg this won't work

r.HandleFunc("/", requireloggedinhandler(MyFunc).ServeHTTP)

Can I enter ServeHTTP as an inherited interface of handler?

1 Answer 1

3

Don't use r.HandleFunc use straight r.Handle("/", MyFunc)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.