0

Im trying to serve static html file in GO. This is how my code in main() looks like.

http.Handle("/", http.FileServer(http.Dir("/static/")))
http.ListenAndServe(":8989", nil)

It works but i dont understand what does static mean! Someone please explain.

David Rojko
  • 27
  • 1
  • 1
  • 4

2 Answers2

1

Here, /static/ is the path to the directory that will be used to serve the requests.

Depending on your setup, you proabably want to set it to a relative path instead of an absolute one…

Elwinar
  • 9,103
  • 32
  • 40
1

This means that whenever you handle requests that serves HTTP requests with the contents of the file system rooted at root, it try to server files declared in http.Dir which uses the operating system's file system implementation.

This means that whenever you access your web server index url it will try to serve files under the operating system /static/ directory.

To serve a directory on disk under an alternate URL path you can use StripPrefix to modify the request URL's path before the FileServer sees it.

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/your/directory/to/static/files"))))
http.ListenAndServe(":8989", nil)
Endre Simo
  • 11,330
  • 2
  • 40
  • 49