Go Notebook
  • Introduction
  • Syntax Helpers
    • Append
    • String
    • Slice
  • Idioms
    • Custom JSON Marshaling
    • Functional Options
    • Type Embedding
    • Laws of Reflection
  • Design Patterns
    • Builder
    • Factory Method
    • Object Pool
    • Singleton
    • Observer
    • Strategy
  • Hello World
    • Getting Started With Go
    • Go Packages
    • Hello World
  • Tic Tac Toe
    • Go Interfaces
    • Go Error Handling
    • Tic Tac Toe
  • HTTP Server
    • HTTP Handler
    • Build a Calculator
    • HTTP Unit Test
  • Concurrency
    • Goroutines
    • Go Concurrency Part 1
    • Go Concurrency Part 2
  • WebSocket Server
    • WebSocket Handler
    • Build a Chatroom
  • User Authentication
    • Go Module
    • Popular Web Libraries
    • React Tools
    • Build an Authentication Server
  • Numerical Computing
    • Gonum
    • Neural Networks
Powered by GitBook
On this page
  1. HTTP Server

HTTP Handler

Inside the HTTP package, there are two handler types, http.Handler and http.HandlerFunc. It is natural to ask why do we have two types of handler.

  • http.Handler is an interface.

  • http.HandlerFunc is a custom function type which takes two arguments, ResponseWriter and *Request.

Any data type that implements the method ServeHTTP will qualify as a HTTP handler. Thus, if we attach a ServeHTTP method to a data type, then that data type is automatically a http.Handler.

In Go, you can attach methods to any data type, even a string or an integer.

type HandlerString string

// ServeHTTP returns the string itself as a response
func (str HandlerString) ServeHTTP(w ResponseWriter, r *Request) {
  w.WriteHeader(http.StatusOK)
  w.Write([]byte(str))
}

Now it is much easier to explain what is a HandlerFunc. Essentially it is a function that implements ServeHTTP.

type HandlerFunc func(ResponseWriter, *Request)

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

If this is still confusing, don't worry. The concept will become clear once we work on calculator server.

PreviousHTTP ServerNextBuild a Calculator

Last updated 6 years ago