O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

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

  1. Open the console and create the folder chapter09/recipe07.
  2. Navigate to the directory.
  3. Create the file welcome.txt with the following content:
        Hi, Go is awesome!
  1. Create the folder html, navigate to it and create the file page.html with the following content:
        <html>          <body>            Hi, I'm HTML body for index.html!          </body>        </html>
  1. Create the static.go file with the following content:
        package main        import (          "net/http"        )        func main() {          fileSrv := http.FileServer(http.Dir("html"))          fileSrv = http.StripPrefix("/html", fileSrv)          http.HandleFunc("/welcome", serveWelcome)          http.Handle("/html/", fileSrv)          http.ListenAndServe(":8080", nil)        }        func serveWelcome(w http.ResponseWriter, r *http.Request) {          http.ServeFile(w, r, "welcome.txt")        }

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