How to Create a Basic Web Server in Go

Go is an exciting programming language for creating modern web applications as well as system software. It swept across the tech industry when it was released and powers services like Docker, Kubernetes, Terraform, Dropbox, and Netflix.

Additionally, Go’s robust collection of built-in packages makes it a great choice for web programming. This article will teach you how to write a basic web server in Go.

Importing the necessary packages

The net/HTTP package provides everything needed to create web servers and clients. This package exposes several useful functions for handling web programming.

You can import it by adding the line below at the top of your source code:

import "net/http"

We will also use the fmt package for formatting strings and Newspaper package for error handling. You can either import them individually as shown above, or factorize all packages using a single import statement:

import (
"fmt"
"log"
"net/http"
)

You can continue to write the main function after importing the required packages. Go ahead and save the source file with a .go extension. If you are using Vim, use the command below to save and exit Vim:

:wq server.go

Write the main function

Go programs live in the main function, appropriately called “main”. You will need to implement the server call here. Add the following lines to your source code and see what they do:

USE VIDEO OF THE DAY
func main() {
http.HandleFunc("https://www.makeuseof.com/", index)
log.Fatal(http.ListenAndServe(":8080", nil))
}

We define the main function using the function keyword. Go has strict rules regarding placement of the opening brace, so make sure the starting brace is on the correct line. The first statement in main defines that all web requests arriving at the root (“https://www.makeuseof.com/”) path will be handled by indexa function of the type http.HandlerFunc.

The second line starts the web server via the http.listenandserve a function. It signals the server to constantly listen for incoming HTTP requests on port 8080 of the host machine. The second parameter of this function is needed to block the program until the end.


Since http.ListenAndServe always returns an error, we wrap this call in a log. Fatal to call. This statement logs all error messages generated on the server side.

Implementing the handler function

As you can see, the main function calls the index handler function to process client requests. However, we have not yet defined this function for our server.

Let’s add the necessary instructions to make the index function usable:

func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, welcome to %s!", r.URL.Path[1:])
}

This function takes two different arguments of type http.ResponseWriter and http.Request. The http.ResponseWriter parameter contains the server’s response to the incoming request, which is in the form of an http.Request object.

The Fprintf function of the fmt package is used to display and manipulate text strings. We use it to display the server’s response to our web requests. Finally, the r.URL.Path[1:] The component is used to retrieve data that comes after the root path.

Add all the pieces together

Your Go web server should be ready once you’ve added all the pieces together. The code should look like the following:

package main
import (
"fmt"
"log"
"net/http"
)
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, welcome to %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("https://www.makeuseof.com/", index)
log.Fatal(http.ListenAndServe(":8080", nil))
}

The first line is needed to compile this Go web server code as an executable file.


Simple web server built with Go

Build web servers with Go

Go’s robust library packages make web programming easy for beginners. You can quickly develop simple web servers with just a few lines of code.

Moreover, the powerful testing features of this programming language also facilitate the implementation of Agile programming methodologies. It is a group of software development strategies based on iterative development and extensive collaboration between teams.


People collaborating at a table
What is the Agile Programming Methodology?

Whether you’re applying for programming jobs or want a more efficient way of working, you need to know Agile.

Read more


About the Author