I'm learning go web programming and understand everthing but I'm getting lost in this 3 apparently simple lines:
fs := http.FileServer(http.Dir("public"))
handler := http.StripPrefix("/static/", fs)
mux.Handle("/static/", handler)
...I have read the go src for the following lines and this is what I can infer:
-
http.Dir("public")
is casting string "public" to type Dir. - then we serve a file (including all its content) with http.FileServer()
- We strip prefix because now we are inside a handleFunc() for fs
- StripPrefix() created a HandlerFunc()
- mux.Handle() registers HandlerFunc() in the mux.
- Deep goes the rabbit hole... and then this goroutine
go c.serve(ctx)
byfunc (srv *Server) Serve(l net.Listener) error {}
- So each static file inside
/public/
dir is served concurrently by our server.
Can someone confirm or explain what exactly is happening in the 3 lines of code.