+++

Assets and Files

This example will show how to serve static files like CSSs, JavaScripts or images from a specific directory.

  1. // static-files.go
  2. package main
  3. import "net/http"
  4. func main() {
  5. fs := http.FileServer(http.Dir("assets/"))
  6. http.Handle("/static/", http.StripPrefix("/static/", fs))
  7. http.ListenAndServe(":8080", nil)
  8. }
  1. $ tree assets/
  2. assets/
  3. └── css
  4. └── styles.css
  1. $ go run static-files.go
  2. $ curl -s http://localhost:8080/static/css/styles.css
  3. body {
  4. background-color: black;
  5. }