Override the mime type of the Go HTTP file server

This example Go program shows how to override the mime type which Go's HTTP server supplies. In this case, the server thinks that everything ending in .t is an application/x-troff file. This overrides that assumption by adding a Content-Type header if the request is for a file ending in .t. In this case, the files ending in .t are actually Perl test files.

package main

import (

// The directory to serve.
var dir = "/home/ben/projects/lingua-ja-moji"
var d = http.Dir(dir)
var fileserver = http.FileServer(d)
var tFile = regexp.MustCompile("\\.t$")

func myfileserver(w http.ResponseWriter, r *http.Request) {
        ruri := r.RequestURI
        if tFile.MatchString(ruri) {
                w.Header().Set("Content-Type", "text/plain")
        fileserver.ServeHTTP(w, r)

func main() {
        http.HandleFunc("/", myfileserver)
        log.Fatal(http.ListenAndServe(":9000", nil))


When compiled and run with

go run omt.go

this will run at http://localhost:9000 or You probably need to change the directory from /home/ben/projects/lingua-ja-moji.

Copyright © Ben Bullock 2009-2018. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer