title: Is it possible to bind a method at runtime?
body: <p>I have the following construct:</p>
  
  <pre><code>type Foo struct {
      Bar func(foo *Foo) bool
  }
  </code></pre>
  
  <p>Because <code>Bar</code> is not really a method, it accepts <code>Foo</code> as parameter (like <code>self</code> in Python's bound methods). I'd however bind it to the struct as a method if there was an easy way. I suspect I could using reflection, but I want to keep it trivial. Is there a trick to bind a function to a struct? How would you do it?</p>
  
  <p>Edit: I'll add a concrete example of what I'm doing.</p>
  
  <pre><code>type Route struct {
      Matcher func(route *Route, r *http.Request) bool
  }
  </code></pre>
  
  <p>Route accepts a custom <code>Matcher</code> function. If not set, a default matcher function is set when the route is registered:</p>
  
  <pre><code>func (mux *ServeMux) HandleRoute(r Route) {
      // ...
  
      // Set default matcher.
      if r.Matcher == nil {
          r.Matcher = mux.RouteMatcher
      }
  
      // ...
  }
  </code></pre>
  
  <p>Then that function is used to do a matching:</p>
  
  <pre><code>func (mux *ServeMux) Match(r *http.Request) Route {
      // ...
  
      if route.Matcher(&amp;route, r) {
          ...
      }
  
      ...
  }
  </code></pre>
  
  <p>The function is not bound to the route. My question is if this is a reasonable/idiomatic way to set a custom callable, or if there is a trick to make the function "bound" to the struct as a method.</p>