Serving gzipped content for Go

The New York Times have released their gzip middleware package for Go.

You just pass your http.HandlerFunc through their GzipHandler and you’re done. It looks like this:

package main

import (

func main() {
    withoutGz := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "text/plain")
        io.WriteString(w, "Hello, World")

    withGz := gziphandler.GzipHandler(withoutGz)

    http.Handle("", withGz)
    http.ListenAndServe("", nil)

Leave a Comment