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/recipe01.
  2. Navigate to the directory.
  3. Create the servertcp.go file with the following content:
        package main        import (          "bufio"          "fmt"          "io"          "net"        )         func main() {          l, err := net.Listen("tcp", ":8080")          if err != nil {            panic(err)          }          for {            fmt.Println("Waiting for client...")            conn, err := l.Accept()            if err != nil {              panic(err)            }            msg, err := bufio.NewReader(conn).ReadString('\n')            if err != nil {              panic(err)            }            _, err = io.WriteString(conn, "Received: "+string(msg))            if err != nil {              fmt.Println(err)            }            conn.Close()          }        }
  1. Execute the code by go run servertcp.go:
  1. Open another Terminal and execute  ...

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