Dante Dante - 1 year ago 86
HTTP Question

Why do I need to use http.StripPrefix to access my static files?


package main

import (

func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)

Directory structure:

%GOPATH%/src/project_name/static/..files and folders ..

Even after reading the documentation I have trouble understanding what exactly
does here.

1) Why can't I access
if I remove

2) What URL maps to
folder if I remove that function?

Answer Source

http.StripPrefix() forwards the handling of the request to one you specify as its parameter, but before that it modifies the request URL by stripping off the specified prefix.

So for example in your case if the browser (or an HTTP client) requests the resource:


StripPrefix will cut the /static/ and forward the modified request to the handler returned by http.FileServer() so it will see that the requested resource is


The Handler returned by http.FileServer() will look for and serve the content of a file relative to the folder (or rather FileSystem) specified as its parameter (you specified "static" to be the root of static files).

Now since "example.txt" is in the static folder, you have to specify a relative path to that to get the corrent file path.

Another Example

This example can be found on the http package documentation page (here):

// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
        http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))


FileServer() is told the root of static files is "/tmp". We want the URL to start with "/tmpfiles/". So if someone requests "/tempfiles/example.txt", we want the server to send the file "/tmp/example.txt".

In order to achieve this, we have to strip "/tmpfiles" from the URL, and the remaining will be the relative path compared to the root folder "/tmp" which if we join gives: