How to do it...

In this recipe, we are going to create a file server that will serve static resources from the filesystem. Perform the following steps:

  1. Create main.css inside a static/css directory, as follows:
$ mkdir static && cd static && mkdir css && cd css && touch main.css
  1. Copy the following content to main.css:
body {color: #00008B}
  1. Create serve-static-files.go, where we will create FileServer, which will serve resources from the static/css directory present on the filesystem for all URL patterns with /static, as follows:
package mainimport (  "fmt"  "html/template"  "log"  "net/http")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")type Person struct {  Name string  Age string}func renderTemplate(w http.ResponseWriter, r *http.Request) ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.