Go is an exciting programming language for building modern web applications as well as system software. It swept the tech industry upon its release and powers services like Docker, Kubernetes, Terraform, Dropbox, and Netflix.

Plus, Go’s solid collection of built-in packages make 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 to handle errors. You can either import them individually as shown above, or factor all packages using a single import statement:

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

You can proceed 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

Writing the main function

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

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 the placement of the opening splint, so make sure the starting splint is on the correct line. The first statement in main defines that all web requests arriving at root (“https://www.makeuseof.com/”) the path will be managed by index, a function of the type http.HandlerFunc.

The second line starts the web server through the http.ListenAndServe 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 necessary to block the program until the end.

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

Implementing the manager function

As you can see, the main function calls the index of the manager’s function to process customer requests. However, we have not yet defined this function for our server.

Let’s add the instructions needed 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.Query. 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 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 parts together

Your Go web server should be ready once you’ve added everything 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 required to compile this Go web server code as an executable file.

Simple web server built with Go

Create 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.

In addition, 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 are applying for programming positions or want a more efficient way of working, you need to know Agile.

Read more


About the Author


Source link

Recommended Posts