O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required