使用golang服务静态文件

I'm trying to develop a simple web application but I'mhaving problem with serving my static files.

The file structure is:

  • main

--main.go

-serve

--listenAndServe.go

--templates

---login.html

---assets

----css

----fonts

----js

my code is this:

    import (
        "log"
        "net/http"
        "time"

        "github.com/gorilla/mux"
    )

    var (
        router = mux.NewRouter()
    )

    func (c *Conn) ListenAndServe() {
        fs := http.FileServer(http.Dir("./templates/assets"))
        http.Handle("/assets/", http.StripPrefix("/assets/", fs))
        router.HandleFunc("/", c.IndexPageHandler)
        router.HandleFunc("/login.html", c.LoginPageHandler)
        log.Println("Listening...")
        http.Handle("/", router)
        muxWithMiddlewares := http.TimeoutHandler(router, time.Minute*30, 
        "Timeout!")
         http.ListenAndServe(":8080", muxWithMiddlewares)
      }

But for some reason when I run it from main.go it serves the html but not the assets. I would really apreciate some tips. Thanks!

Try This:

mux.Handle("/static/", http.StripPrefix("/static", fileServer))

Note that static, in your case assets only has a single forward slash within the stripPreFix function.

Hope this helps.