How to do it...

In this recipe, we are going to update the handleRequest method in the program to write data back to the client. Perform the following steps:

  1. Create tcp-server-write-data.go and copy the following content:
package mainimport (  "bufio"  "fmt"  "log"  "net")const (  CONN_HOST = "localhost"  CONN_PORT = "8080"  CONN_TYPE = "tcp")func main() {  listener, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)  if err != nil   {    log.Fatal("Error starting tcp server : ", err)  }  defer listener.Close()  log.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)  for   {    conn, err := listener.Accept()    if err != nil     {      log.Fatal("Error accepting: ", err.Error())    }    go handleRequest(conn)  }}func handleRequest(conn net.Conn) { message, err := bufio.NewReader(conn).ReadString('\n') ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.