Building a Web Server via Golang

Building a Web Server via Golang

Let us build a web server , this implementation is quite interesting.

I have chosen Go language because it has much less compilation time and verbosity( things expressed in less number of words) as compared to any other programming language.

Golang development has awesome concurrency support , and since it is statically typed language, maximum errors are caught at compilation time.

We will be developing a web server that can accept GET request and serve a response. Web Server is a program running on any host with an IP (static or dynamic) with a specific port. We will send data (packets) to the server as clients , to which server responds with requested data.

Web server working

What you must have is Golang installed with compiler and code editor.

We will be using Go-lang package “net” to create HTTP/HTTPS server and make the client requests.(https://golang.org/src/net/net.go)

This package (net) consists of an interface to support network I/O like TCP/IP ,UDP,DNS resolution and emails. We will also be using HTTP package which includes package like ListenAndServe method, which starts the server and listens to incoming requests.

ListenAndServe will accept two arguments:

  1. Address:This is the combination of the IP address and port of the machine in a string format (‘192.168.1.1:2000’).If only port is given in arguments (‘:9000’) , then this port becomes accessible from all the IP addresses across the system.
  2. Handler:This argument is of type Handler interface.Internally this interface uses serveHTTP method , which gets called when ListenAndServe method is executed.
  3. ServeHTTP method: signature for this method is => ServeHTTP(res http.ResponseWriter, req *http.Request) . This will accept the request and returns the response.

Code for Server with request and response

Checking for the response from the command line

Checking the output in the browser

for checking the code, you can refer this repository:

[aniforverizon/Golangserver
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…github.com](https://github.com/aniforverizon/Golangserver/blob/main/Server_1.go "github.com/aniforverizon/Golangserver/blob/..")

So this was a simple server implementation , now lets customise this by adding a route based control mechanism for requests like (/index.html or /api/myguests or /test) . For this we will be using SeverMux (https://golang.org/pkg/net/http/#ServeMux) structure available in the HTTP package , which will take input as a function for a specific route.

Customised route based server implementation

Output of the customized server

For checking out the code , refer to this repository:

[aniforverizon/Golangserver
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…github.com](https://github.com/aniforverizon/Golangserver/blob/main/Server_2.go "github.com/aniforverizon/Golangserver/blob/..")

I will be publishing more Go-lang based simple solutions. For any further Queries or anything related to this article or Golang you can DM me on Linkedin or instagram id=acanubhav94.

[Anubhav Chaturvedi - DevOps Solutions Architect - Reliance Jio | LinkedIn
Worked as an Independent Contractor for roles like Blockchain Development(Hyperledger ) and DevOps Engineer including…linkedin.com](https://www.linkedin.com/in/anubhav-chaturvedi-a7465a72/ "linkedin.com/in/anubhav-chaturvedi-a7465a72")