Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Go Web Development Cookbook

You're reading from   Go Web Development Cookbook Build full-stack web applications with Go

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher Packt
ISBN-13 9781787286740
Length 338 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Arpit Aggarwal Arpit Aggarwal
Author Profile Icon Arpit Aggarwal
Arpit Aggarwal
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Creating Your First Server in Go 2. Working with Templates, Static Files, and HTML Forms FREE CHAPTER 3. Working with Sessions, Error Handling, and Caching in Go 4. Writing and Consuming RESTful Web Services in Go 5. Working with SQL and NoSQL Databases 6. Writing Microservices in Go Using Micro – a Microservice Toolkit 7. Working with WebSocket in Go 8. Working with the Go Web Application Framework – Beego 9. Working with Go and Docker 10. Securing a Go Web Application 11. Deploying a Go Web App and Docker Containers to AWS 12. Other Books You May Enjoy

Creating a simple TCP server

Whenever you have to build high performance oriented systems then writing a TCP server is always the best choice over an HTTP server, as TCP sockets are less hefty than HTTP. Go supports and provides a convenient way of writing TCP servers using a net package, which we will be covering in this recipe.

How to do it...

In this recipe, we are going to create a simple TCP server that will accept a connection on localhost:8080. Perform the following steps:

  1. Create tcp-server.go and copy the following content:
package main
import
(
"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())
}
log.Println(conn)
}
}
  1. Run the program with the following command:
$ go run tcp-server.go

How it works...

Once we run the program, the TCP server will start locally listening on port 8080.

Let’s understand what each line in the program means:

  • package main: This defines the package name of the program.
  • import ( "log" "net"): This is a preprocessor command that tells the Go compiler to include all files from the log and net package.
  • const ( CONN_HOST = "localhost" CONN_PORT = "8080" CONN_TYPE = "tcp" ): We declare constants in a Go program using the const keyword. Here, we declare three constants—one is CONN_HOST with a value of localhost, another one is CONN_PORT with a value as 8080, and lastly CONN_TYPE with a value as tcp.

Next, we declared the main() method from where the program execution begins. As this method does a lot of things, let’s understand it line by line:

  • listener, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT): This creates a TCP server running on localhost at port 8080.
  • if err != nil { log.Fatal("Error starting tcp server: ", err) }: Here, we check if there is any problem in starting the TCP server. If there is, then log the error and exit with a status code of 1.
  • defer listener.Close(): This defer statement closes a TCP socket listener when the application closes.

Next, we accept the incoming request to the TCP server in a constant loop, and if there are any errors in accepting the request, then we log it and exit; otherwise, we simply print the connection object on the server console, as follows:

for 
{
conn, err := listener.Accept()
if err != nil
{
log.Fatal("Error accepting: ", err.Error())
}
log.Println(conn)
}
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime