Today, we are going to demonstrate how one could serve static site/ content using golang’s net/http or using gorilla/mux. Let us create base for this. First we would create a directory
Now lets bootstrap our project by puting in some example files
Now let us put in some stuff in assets/index.html
Put in some content in assets/css/application.css as well
Okay, so we are done with our example static site, now we need to serve this using golang http package found at net/http. Put code as mentioned below in http_ex.go
So what we did here is mounted our filesystem at root path /, calling http.StripPrefix was not necessary here but, it becomes necessary when we deploy it at subpath like assets. No sit back and fire go run http_ex.go and browse localhost:3000
All done, now to to same using gorilla/mux, we would touch upon a new file mux_ex.go. Content of the file would look like
Again PathPrefix was not required here but it would, when one would like to deploy on subpath like /blog
Entire codebase for this post can be found on GoStatic@github
About The Author
I am Pankaj Baagwan, a System Design Architect. A Computer Scientist by heart, process enthusiast, and open source author/contributor/writer. Advocates Karma. Love working with cutting edge, fascinating, open source technologies.
To consultPankaj Bagwan on System Design, Cyber Security and Application Development, SEO and SMO, please reach out at me[at]bagwanpankaj[dot]com
For promotion/advertisement of your services and products on this blog, please reach out at me[at]bagwanpankaj[dot]com