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/recipe09.
  2. Navigate to the directory.
  3. Create the file redirect.go with the following content:
        package main        import (          "fmt"          "log"          "net/http"        )        func main() {          log.Println("Server is starting...")          http.Handle("/secured/handle",               http.RedirectHandler("/login",                       http.StatusTemporaryRedirect))          http.HandleFunc("/secured/hadlefunc",                func(w http.ResponseWriter, r *http.Request) {            http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)          })          http.HandleFunc("/login", func(w http.ResponseWriter,                          r *http.Request) {            fmt.Fprintf(w, "Welcome user! Please login!\n")          })          if err := http.ListenAndServe(":8080", nil); err != nil {            panic(err)          }        }
  1. Execute the code by go run redirect.go.
  2. Use curl ...

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