Book Image

Go Web Development Cookbook

By : Arpit Aggarwal
Book Image

Go Web Development Cookbook

By: Arpit Aggarwal

Overview of this book

Go is an open source programming language that is designed to scale and support concurrency at the language level. This gives you the liberty to write large concurrent web applications with ease. From creating web application to deploying them on Amazon Cloud Services, this book will be your one-stop guide to learn web development in Go. The Go Web Development Cookbook teaches you how to create REST services, write microservices, and deploy Go Docker containers. Whether you are new to programming or a professional developer, this book will help get you up to speed with web development in Go. We will focus on writing modular code in Go; in-depth informative examples build the base, one step at a time. You will learn how to create a server, work with static files, SQL, NoSQL databases, and Beego. You will also learn how to create and secure REST services, and create and deploy Go web application and Go Docker containers on Amazon Cloud Services. By the end of the book, you will be able to apply the skills you've gained in Go to create and explore web applications in any domain.
Table of Contents (13 chapters)

Writing data to a TCP connection

Another common, as well as important, scenario in any web application is to send the data back to the client or responding to the client. Go provides a convenient way to write a message on a connection as bytes, which we will be covering in this recipe.

Getting ready...

As we have already created a TCP server that reads incoming connection data in the previous recipe, we will just update it to write the message back to the client.

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 main
import
(
"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')
if err != nil
{
fmt.Println("Error reading: ", err.Error())
}
fmt.Print("Message Received:", string(message))
conn.Write([]byte(message + "\n"))
conn.Close()
}
  1. Run the program with the following command:
$ go run tcp-server-write-data.go

How it works...

Once we run the program, the TCP server will start locally listening on port 8080. Execute an echo command from the command line, as follows:

$ echo -n "Hello to TCP server\n" | nc localhost 8080

This will give us the following response from the server:

Hello to TCP server

Let’s look at the changes we introduced in this recipe to write data to the client. Everything in handleRequest is exactly the same as in the previous recipe except we introduced a new line that writes data as a byte array to the connection, as follows:

func handleRequest(conn net.Conn) 
{
...
conn.Write([]byte(message + "\n"))
...
}