Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:22:34+00:00 2026-06-16T13:22:34+00:00

In Golang, how do I serve static content out of the root directory while

  • 0

In Golang, how do I serve static content out of the root directory while still having a root directory handler for serving the homepage.

Use the following simple web server as an example:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HomeHandler) // homepage
    http.ListenAndServe(":8080", nil)
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "HomeHandler")
}

If I do

http.Handle("/", http.FileServer(http.Dir("./")))

I receive a panic saying that I have two registrations for “/”. Every Golang example I’ve found on the internet suggests serving their static content out of different directories, but that doesn’t help much for things like sitemap.xml, favicon.ico, robots.txt and other files which are by-practice or mandated to always be served out of the root.

The behavior I seek is the behavior which is found in most web servers such as Apache, Nginx, or IIS, where it first traverses your rules, and if no rule is found it looks for an actual file, and if no file is found it 404s. My guess is that instead of writing a http.HandlerFunc, I need to write a http.Handler which checks if I am referencing a file with an extension, and if so checks for file existence and serves the file, otherwise it 404s or serves the homepage is the request was for “/”. Unfortunately I’m not certain how to even begin such a task.

Part of me says I’m massively over-complicating the situation which makes me think that I am missing something? Any guidance would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:22:35+00:00Added an answer on June 16, 2026 at 1:22 pm

    One thing I thought of that might help you is that you can create your own ServeMux. I added to your example so that chttp is a ServeMux that you can have serve static files. The HomeHandler then checks to see if it should serve a file or not. I just check for a “.” but you could do a lot of things. Just an idea, might not be what you are looking for.

    package main
    
    import (
        "fmt"
        "net/http"
        "strings"
    )   
    
    var chttp = http.NewServeMux()
    
    func main() {
    
        chttp.Handle("/", http.FileServer(http.Dir("./")))
    
        http.HandleFunc("/", HomeHandler) // homepage
        http.ListenAndServe(":8080", nil)
    }   
    
    func HomeHandler(w http.ResponseWriter, r *http.Request) {
    
        if (strings.Contains(r.URL.Path, ".")) {
            chttp.ServeHTTP(w, r)
        } else {
            fmt.Fprintf(w, "HomeHandler")
        }   
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having read the following at http://golang.org/doc/effective_go.html#arrays ... Arrays are values. Assigning one array to
Here is an Example of the app. The essential code is in: golang-code/handler/handler.go (After
I use a GO server (golang.org), which does have good support for encryption and
Given the following example: http://play.golang.org/p/owvJ8Oi77S func produce(dataChannel chan int) { for i := 0;
I'm trying out LiteIDE for golang and there are no docs to speak of
I tried to use this tutorial with Golang: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ The go-version opens the window
I was following 'A tour of GO` on http://tour.golang.org . The table 15 has
I'm having some trouble with strings in Golang. It seems that they don't get
I'm new to golang , and want to use it to build a web
I want to open a given file directory/subdirectory/file.txt in golang. What is the recommended

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.