Assets and Files

Hey, Philipp here!
I'd like to tell you, that my platform Go Web Examples Courses just launched. Enjoy easy to follow video courses about web devlopment in Go. Make sure to check out the special offer I have for early supporters.
We'll see us over there! :)
Learn more

Assets and Files

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

// static-files.go
package main

import "net/http"

func main() {
    fs := http.FileServer(http.Dir("assets/"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))

    http.ListenAndServe(":8080", nil)
}
$ tree assets/
assets/
└── css
    └── styles.css
$ go run static-files.go

$ curl -s http://localhost:8080/static/css/styles.css
body {
    background-color: black;
}