What is HTTP HandlerFunc in Go? Complete Guide with Examples

Robin
Updated on November 22, 2023

The http.HandlerFunc() converts a function to the http.Handler type so that it can be used as a normal handler in the http.Handle() method.

In simple words, when you pass a function to the http.HandlerFunc(), it implements the http.Handler type automatically by adding ServeHTTP() method.

You don't have to write ServeHTTP() method manually for your function. Therefore, your normal function becomes a handler.

          package main

import (
	"fmt"
	"net/http"
)

// MyHandler is a simple HTTP handler function.
func MyHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, this is my handler!")
}

func main() {
    // Convert MyHandler function to a HandlerFunc
    handlerFunc := http.HandlerFunc(MyHandler)

    // Use handlerFunc as an HTTP handler
    http.Handle("/myhandler", handlerFunc)

    // Start the web server
    fmt.Print("Server started: http://localhost:8000")
    http.ListenAndServe(":8000", nil)
}

        

Syntax

The HandlerFunc is a function type that takes two parameters: a ResponseWriter and a pointer to a Request.

          type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
	f(w, r)
}

        

This type has the ServeHTTP() method. It calls the underlying function f, which is of type HandlerFunc.

By having this method, the HandlerFunc type satisfies the http.Handler interface.


Converting A Function to a Handler

If you want to convert a function to a handler, your function must follow the HandlerFunc function type.

Create a function that takes two parameters: a ResponseWriter and a pointer to a Request.

          // MyHandler is a simple HTTP handler function.
func MyHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, this is my handler!")
}

        

Here I have a MyHandler() function that satisfies the HandlerFunc signature.

Now I can pass this function to http.HandlerFunc(), it will convert my MyHandler() function to a handler and return it.

          // Convert MyHandler function to a HandlerFunc
handlerFunc := http.HandlerFunc(MyHandler)

        

Here I am calling http.HandlerFunc() and passing MyHandler function to it. The handlerFunc variable is storing the returned handler.

As you already know, if you want to satisfy the http.Handler interface, you have to add the ServeHTTP() method.

The http.HandlerFunc() does exactly that for you in the background. It adds the ServerHTTP() method to your function so that it satisfies the http.Handler interface.


Using HandlerFunc in HTTP Routes

Here the handlerFunc variable holds the handler that satisfies http.Handler interface, you can use it in the http.Handle() method to process HTTP requests.

          package main

import (
	"fmt"
	"net/http"
)

// MyHandler is a simple HTTP handler function.
func MyHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, this is my handler!")
}

func main() {
    // Convert MyHandler function to a HandlerFunc
    handlerFunc := http.HandlerFunc(MyHandler)

    // Use handlerFunc as an HTTP handler
    http.Handle("/myhandler", handlerFunc)

    // Start the web server
    fmt.Print("Server started: http://localhost:8000")
    http.ListenAndServe(":8000", nil)
}

        

I am calling http.Handle() method for the route "/myhandler" and passing handlerFunc variable to it.

When a request hits this route, Golang will execute the ServeHTTP() method from your handlerFunc handler which will call the MyHandler() function.

That's how you can convert a function that has the correct signature to an HTTP handler with http.HandlerFunc and use it for the http.Handle function to register a specific route.

Related Posts