Serving static (css) files

Hello all,

I’m new to Go but already exited, to practice I started to build a simple blog app, and everything is working smooth, except for the css.

I have been trying lot’s of examples for days now but it just keeps replying with a 404.
These are two of the examples I’ve tried and both examples are explained on other resources in the same way:

I’ve tried both with and without Gorilla Mux, both examples fail as well as my own project down below. What is going wrong here?

Folder structure:



package main

import (

func main() {
	r := mux.NewRouter()
	// Index
	r.HandleFunc("/", home.DashboardHandler)
	// Posts
	r.HandleFunc("/posts/", posts.Index)
		p := r.PathPrefix("/posts").Subrouter()
		p.HandleFunc("/{id:[0-9]+}/{title}", posts.Single)
		//and some more subroutes
	// Categories
	r.HandleFunc("/categories/", categories.Index)
		c := r.PathPrefix("/categories").Subrouter()
		c.HandleFunc("/{id:[0-9]+}/{title}", categories.Single)
		//and some more subroutes

	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))

	http.Handle("/", r)

	http.ListenAndServe(":8080", nil)



{{define "header"}}
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link href="/static/style.css" type="text/css" rel="stylesheet"/>

Thanks for helping out!

The problem is your code expects you to be in th directory above static/

How are you running this program? If you are using go run

  1. Please stop using go run, it’s a bad habit
  2. Go run always runs your program in a random temporary directory.

I recommend using go build or go install to build the program, then change into the correct directory before executing your program.

1 Like

Hello Dave,

I use the “Build main.go and run” function inside Webstorm. I have now builded it from the terminal and ran the exec, but still the browser can’t locate the file:
GET http://localhost:8080/static/style.css 404 (Not Found)

If I use the examples from the links it’s also the same using build and the run the exec.

Edit: It’s not displaying anything at all at the moment. Just: open /Users/jschalkwijk/GolangTraining/blog/templates/header.html: no such file or directory
I do have a connection with my database and no errors in my terminal. But now it doesn’t seem to find the template files

Why not add a check on startup to ensure that the static files are where you expect them. Something like

_, err := os.Stat(filepath.Join(".", "static", "style.css"))

If err is not nil, exit the program and try to figure out why


It’s saying that there is no such file or dir, but even if I put it the system path, it say’s the same
panic: stat Users/jorn/Documents/Golang/src/ no such file or directory

I’m not a professional programmer yet so I don’t really know where to look now.

For anyone who wants to check out more of the code to maybe grasp the problem:

I think @dfc implied this but im not sure if it came across clearly, so I’m gonna explicitly rephrase it:

It is critically important what directory you’re in when you’re starting your program.

In This tutorial the tutor phrased it:

Wherever you are in your directory structure when you [run your program], that becomes your root for your server.

For example:
Looking at this error

And this line in your code:

var templates, _ = filepath.Abs("../jschalkwijk/GolangTraining/blog/templates")

I would assume you were in “/Users/anydirectory/” when you started your program (that time), which means “/Users/anydirectory/” was the working directory for your program which according to the docs meant template was equal to /Users/jschalkwijk/GolangTraining/blog/templates which would probably lead nowhere useful.

I think I’d need a full view of your console output to be more sure of this, but that might be a place to start looking.


This is a common stumbling block for web programming with Go.

When I first started learning web dev with Go, I also spent hours getting resources (css, js, images) to serve.

You can peruse this directory:​

It has working examples of CSS being served.

Specifically, this works:​

This is also a nice example which isn’t seen much in examples that are out there on the web:​

If this doesn’t help, send me the link to your code on github and I’ll get it working.

1 Like

Always use absolute paths. Change them via a config file or environmental variables - e.g. os.Getenv("ASSET_DIR") - where ASSET_DIR is set to /Users/you/code/project/assets/ and not just ./assets/.

Relative paths to assets, configuration, etc. are a trap :smile:

1 Like

Thanks, after some trial and error I got it to work, and also have a better understanding of the how and why!

@dfc you where right, I just didn’t fully understand what you meant :slight_smile: @Jaytn Thanks for clarifying and the link to Todd’s lecture, this will help overcome future problems. And @toddmcleod as always thanks for your code base and quick reply, a lot of examples to work from!

I have had to change the path to:

r.PathPrefix("/GolangTraining/blog/static/css").Handler(http.StripPrefix("/GolangTraining/blog/static/css", http.FileServer(http.Dir("./GolangTraining/blog/static/css"))))

And the CSS path to:
<link href="/GolangTraining/blog/static/css/style.css" type="text/css" rel="stylesheet"/>

And then I changed directory into and ran the main.go from there.

Thanks a lot!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.