Assets and Files

This example will show how to serve static files like CSS, JavaScript or images from a specific directory using GOLang net/http package.

// 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)

Create /assets/css/ folders and make styles.css file for test

$ tree assets/
└── css
    └── styles.css

Run GO file and test result

$ go run static-files.go

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