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/recipe04.
  2. Navigate to the directory.
  3. Create the http.go file with the following content:
        package main        import (          "fmt"          "io/ioutil"          "net/http"          "net/url"          "strings"        )        type StringServer string        func (s StringServer) ServeHTTP(rw http.ResponseWriter,                                        req *http.Request) {          req.ParseForm()          fmt.Printf("Received form data: %v\n", req.Form)          rw.Write([]byte(string(s)))        }         func createServer(addr string) http.Server {          return http.Server{            Addr: addr,            Handler: StringServer("Hello world"),          }        }        const addr = "localhost:7070"        func main() {          s := createServer(addr)          go s.ListenAndServe()          useRequest()          simplePost()        }        func simplePost() {          res, err := http.Post("http://localhost:7070", "application/x-www-form-urlencoded", ...

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