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 chapter07/recipe08.
  2. Navigate to the directory.
  3. Create the redirects.go file with the following content:
        package main        import (          "fmt"          "net/http"        )        const addr = "localhost:7070"        type RedirecServer struct {          redirectCount int        }        func (s *RedirecServer) ServeHTTP(rw http.ResponseWriter,                                          req *http.Request) {          s.redirectCount++          fmt.Println("Received header: " +                       req.Header.Get("Known-redirects"))          http.Redirect(rw, req, fmt.Sprintf("/redirect%d",                        s.redirectCount), http.StatusTemporaryRedirect)        }        func main() {          s := http.Server{            Addr: addr,            Handler: &RedirecServer{0},          }          go s.ListenAndServe()          client := http.Client{}          redirectCount := 0          // If the count of redirects is reached // than return error. ...

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