title: How HandlerFunc(f) convert a function to an interface type?
body: <p>When check following code, got a doubt with type convert from function to interface.</p>
  
  <hr>
  
  <h2>Code</h2>
  
  <p><strong>http_hello.go:</strong></p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "log"
      "net/http"
  )
  
  // hello http,
  func helloHttp() {
      // register handler,
      http.Handle("/", http.HandlerFunc(helloHandler))
  
      // start server,
      err := http.ListenAndServe(":9090", nil)
      if err != nil {
          log.Fatal("ListenAndServe:", err)
      }
  
  }
  
  // handler function - hello,
  func helloHandler(w http.ResponseWriter, r *http.Request) {
      fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path)
  }
  
  func main() {
      helloHttp()
  }
  </code></pre>
  
  <p>The above code works.</p>
  
  <p><em>(Then I tried to write a small program to check is this a general feature, but it won't work, check following code)</em></p>
  
  <p><strong>func_to_intf.go:</strong></p>
  
  <pre><code>package main
  
  import (
      "fmt"
  )
  
  // an interface,
  type Adder interface {
      add(a, b int) int
  }
  
  // alias of a function signature,
  type AdderFunc func(int, int) int
  
  // a simple add function,
  func simpleAdd(a, b int) int {
      return a + b
  }
  
  // call Adder interface to perform add,
  func doAdd(a, b int, f Adder) int {
      return f.add(a, b)
  }
  
  func funcToIntf() {
      fa := AdderFunc(simpleAdd)
      fmt.Printf("%#v, type: %T\n", fa, fa)
  
      a, b := 1, 2
      sum := doAdd(a, b, fa)
      fmt.Printf("%d + %d = %d\n", a, b, sum)
  }
  
  func main() {
      funcToIntf()
  }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <blockquote>
    <p>./func_to_intf.go:30:14: cannot use fa (type AdderFunc) as type Adder
    in argument to doAdd:     AdderFunc does not implement Adder (missing add
    method)</p>
  </blockquote>
  
  <hr>
  
  <h2>Questions</h2>
  
  <ol>
  <li><code>http.HandlerFunc(helloHandler)</code> get a value of type <code>http.Handler</code>, since that's what <code>http.Handle()</code> expect, is that correct?</li>
  <li>If yes, then means it convert a function into a value of an interface type, how did that happen?  
  
  <ul>
  <li>Is this a built-in feature of go?<br>
  I did a test (as in <code>func_to_intf.go</code> above), and seems not.</li>
  <li>Or, is <code>http.HandlerFunc</code>'s special implementation achieve that?</li>
  </ul></li>
  </ol>
  
  <hr>
  
  <h2>@Update - Summary</h2>
  
  <p><em>(Though the answer(s) addressed the questions pretty well, but after reviewing &amp; more testing, there are several other go features required to totally erase the original doubt, as following.)</em></p>
  
  <ul>
  <li>Function type.<br>
  Function is value, and it has type.<br>
  Function type could be defined via <code>type</code> keyword on a function signature.<br>
  e.g <code>type AdderFunc func(int, int) int</code></li>
  <li>Type convertor <code>Ｔ(v)</code> on function.<br>
  Any function could be converted to a function type with the same signature, just via <code>T(v)</code>, use function type name as <code>T</code>, and actual function as <code>v</code>.<br>
  Then when the new value is called, the actual function <code>v</code> is called.<br>
  e.g <code>fa := AdderFunc(simpleAdd)</code><br>
  <em>(this is blur to me before asking the question, and that's one of the main reason I was confused)</em>.</li>
  </ul>