Chapter 6 -HTTP package in golang
Building our first Webservice with Go (Part 3)

The following list is the previous chapters of this series:
- Chapter 1 — Introduction to Microservices
- Chapter 2 — Introduction to Microservices (Part 2)
- Chapter-3 Domain-driven design and microservices
- Chapter -4 Advantages of using Go for web development
- Chapter — 5 Understanding HTTP Protocols and REST APIs
I recommend you take a look at the previous chapters if you have not read them yet. That will help you to get more knowledge in this wonderful world of “Microservices architecture”.
Structure
In this chapter, the following topics will be covered:
- HTTP package in golang
- Overview of the net/http package in Go for handling HTTP requests and responses.
- Learning how to create HTTP servers and handle routing in Go.
HTTP package in golang
You have completed our extensive theory to introduce the microservice world. Now with all that acquired knowledge from the previous topics and chapters, it’s time to focus on the primary purpose of this series of articles. In this section, we will take a closer look at using Git and Golang as the programming language to build microservices.
Before going to elaborate on microservices let’s understand what golang provides to build web services.
This section is dedicated to exploring the HTTP package in Golang. We will delve into one of the core packages of the Go standard library, which plays a pivotal role in web development and communication over the Internet. The HTTP package empowers developers to build robust and performant web applications, enabling seamless interaction between clients and servers.
Throughout this topic, we will explore the robust features and functionalities offered by Golang’s HTTP package. From efficiently handling HTTP requests and responses to working with cookies, headers, and more, we will cover all aspects to establish a strong foundation for the next chapter, which will focus on building microservices with Go.
By the end of this section, we will have a step-by-step code to build a web service, allowing us to apply the knowledge gained and create a practical implementation of the concepts discussed.
Describing the HTTP package
The “net/http” package is an indispensable component of the standard library from Go programming language, providing developers with the necessary tools to build web applications and HTTP servers with ease and efficiency. This package enables the seamless creation of web servers that can handle incoming requests, process routes, generate responses, interact with cookies, and even support the TLS/SSL protocol for secure connections. Its user-friendly nature and high performance make it an excellent choice for developing web applications in the Go programming language.
With a rich set of functions and structures, the net/http package facilitates managing the lifecycle of an HTTP request and response. It empowers developers to read and write headers, access request data, and respond with content in various formats such as text, JSON, and images. Additionally, the package facilitates the use of middleware, making it convenient to integrate requests and responses straightforwardly.
To finalize the description, the net/http package serves as a powerful and efficient tool for building Go web applications, and it is widely embraced by the community for developing robust and scalable HTTP servers.”
You can find the complete guide and description of the package on the Go official website https://pkg.go.dev/net/http.
In the following code block, is shown one example of a web service created with the net/http package. The server is created to listen on port 8080 and responds with “Hello world!” for any request that hits the root path “/”.
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, world!")
}Explaining the code:
- http.HandleFunc(“/”, handler) This function is utilized to bind a handler function to a specific route on the HTTP server. In this small example, the function “handler” has been associated with the root path “/”. Consequently, this function will be executed whenever a request is received at the root path of the server.
- http.ListenAndServe(“:8080”, nil) Starts an HTTP server on the specified port. For this example, the server is being started on port 8080. The second argument to this function indicates that a custom router is not being used, so routing will be handled automatically using functions registered via http.HandleFunc.
- func handler(w http.ResponseWriter, r *http.Request) This is the handler function that is defined to handle requests in the root path “/”. This function defines two arguments: http.ResponseWriter and *http.Request.
- w http.ResponseWriter It is an interface that allows writing the HTTP response that will be sent to the client. In this example, is used the Fprintln function from the fmt package to write “Hello, world!”.
- r *http.Request It is a structure that contains all the information about the incoming HTTP request, such as the HTTP method, headers, URL parameters, etc. However.
As can be seen with very few lines of code we can have a web service running. At the moment it will seem very simple but don’t worry in the next section we will create a more elaborate example, where each of the functionalities will be explained step by step and the contributions of the net/http package will be seen in more detail.
With just a few lines of code, we can have a fully functional web service up and running, although it may seem a simple example at this moment, in the next section, we will create a more elaborate example where each part of its functionality will be explained, and will the net/http package will be analyzed on detail to see its valuable contributions in action.
Next readings …
Wait for Chapter 7 “Step-by-step guide on building a simple web service using Go”.






