Connecting to the HTTP server
The previous recipe, Connecting to the remote server, gave us an insight into how to connect the TCP server at a lower level. In this recipe, communication with the HTTP server at a higher level will be shown.
How to do it...
- Open the console and create the folder
chapter07/recipe04. - Navigate to the directory.
- Create the
http.gofile 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"),
}...