Serving Images along with Template Parsing

I am having a problem getting images to display on served html template pages from a static directory on my machine.

Here is my code:

import (

	data ""

func main() {
	multiplex := mux.NewRouter()
	multiplex.Handle("/Public/", http.FileServer(http.Dir("Public")))
	multiplex.HandleFunc("/", WelcomeHandler)
	multiplex.HandleFunc("/directory/", DirectoryHandler)
	multiplex.HandleFunc("/dashboard/{userId}/", DashboardHandler)

	http.ListenAndServe("", multiplex)

// WelcomeHandler Handles requests for welcome Page
func WelcomeHandler(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("Templates/mainlayout.html", "Templates/publicnav.html")
	t.Execute(w, "Welcome!")


// DirectoryHandler Handles requests for directory
func DirectoryHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "<h1>Directory Page!</h1>")

// DashboardHandler Handles requests for dashboard
func DashboardHandler(w http.ResponseWriter, r *http.Request) {

	fmt.Fprintf(w, fmt.Sprintf("<h1>Dashboard! %v</h1>", "Guy Fiori"))

Is there something I’m missing, or is that not what http.FileServer is for?

iirc gorilla/mux does explicit path handling, so multiplex.Handle("/Public/", http.FileServer(http.Dir("Public"))) will only match to not

Try PathPrefix -

You want something like:


And you likely need to also use the StripPrefix handler - giving you something like below (I haven’t tested it though)

multiplex.PathPrefix("/Public/").Handler(http.StripPrefix("/Public/", http.FileServer(http.Dir("Public")))))
1 Like

Thank you for that, now it is working :slight_smile:
I thought it might have something to do with gorilla/mux’s path handling

1 Like

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