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/recipe02.
  2. Navigate to the directory.
  3. Create the tcpclient.go file with the following content:
        package main        import (          "bufio"          "context"          "fmt"          "io"          "net"          "net/http"          "time"        )         type StringServer string        func (s StringServer) ServeHTTP(rw http.ResponseWriter,                                        req *http.Request) {          rw.Write([]byte(string(s)))        }        func createServer(addr string) http.Server {          return http.Server{            Addr: addr,            Handler: StringServer("HELLO GOPHER!\n"),          }       }       const addr = "localhost:7070"       func main() {         s := createServer(addr)         go s.ListenAndServe()         // Connect with plain TCP         conn, err := net.Dial("tcp", addr)         if err != nil {           panic(err)         }         defer conn.Close() _, err = io.WriteString(conn, "GET / HTTP/1.1\r\nHost: ...

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