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/recipe08.
  2. Navigate to the directory.
  3. Create the file template.tpl with the following content:
        <html>          <body>            Hi, I'm HTML body for index.html!          </body>        </html>
  1. Create the file dynamic.go with the following content:
        package main        import "net/http"        import "html/template"        func main() {          tpl, err := template.ParseFiles("template.tpl")          if err != nil {            panic(err)          }          http.HandleFunc("/",func(w http.ResponseWriter, r *http.Request){            err := tpl.Execute(w, "John Doe")            if err != nil {              panic(err)            }          })          http.ListenAndServe(":8080", nil)        }
  1. Execute the code by go run dynamic.go.
  2. Check the URL http://localhost:8080 and see the output:

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