CSS is not one of the file types identified by DetectContentType. You should match that based on the file extension instead. See also mime.TypeByExtension. I typically end up doing something like this, when I know I’m serving a limited set of files:
func (s *staticsServer) mimeTypeForFile(file string) string {
// We use a built in table of the common types since the system
// TypeByExtension might be unreliable. But if we don't know, we delegate
// to the system.
ext := filepath.Ext(file)
switch ext {
case ".htm", ".html":
return "text/html"
case ".css":
return "text/css"
case ".js":
return "application/javascript"
// ...
default:
return mime.TypeByExtension(ext)
}
}
I’m not sure if the reason you are trying to get the content type is because you are trying to serve your assets or not, but if you are, here’s a simple structure that you can use instead of what you are currently doing:
main.go index.html assets/ - styles.css
main.go
package main
import (
"html/template"
"log"
"net/http"
)
var tmpls = template.Must(template.ParseFiles("index.html"))
// Index page handler
func IndexHandler(w http.ResponseWriter, r *http.Request) {
if err := tmpls.ExecuteTemplate(w, "index.html", nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
// Serve the index page
http.HandleFunc("/", IndexHandler)
// Serve assets
http.Handle("/assets/", http.StripPrefix("/assets/",
http.FileServer(http.Dir("assets/"))))
log.Fatalln(http.ListenAndServe(":8000", nil))
}
Since you’re serving up CSS files using Go, you might be interested in my gzipped.FileServer project, which is a drop-in replacement for the standard Go http.FileServer (minus directory browsing) and allows you to serve zopfli-precompressed versions of your CSS files. With Bootstrap, this cuts the (already minimized) CSS file from 122KB bytes to 19KB during transit.