Home DRIVERS DOWNLOADServerServer Knowledge How to build a basic web server using Go

How to build a basic web server using Go

by Total Drivers

Go is an interesting programming language for building modern web applications as well as system software. It created a huge buzz after its release and powers services like Docker, Kubernetes, Terraform, Dropbox, and  Netflix.

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

Import the necessary packages

The net/HTTP package provides everything needed to create a web server and client. This package presents some useful functions for dealing with web programming.

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

import "net/http"

The article will also use the  fmt  package for string formatting and the  log  package for error handling. You can import them individually as shown above, or import all packages using a single import statement:

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

You can proceed to write the main function after importing the necessary packages. Go ahead and save the source file with the  .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 reside directly in the main function, aptly named “main”. You will need to make a call to  server  here. Add the following lines to the source code and see what they do:

func main() {
http.HandleFunc("/", index)
log.Fatal(http.ListenAndServe(":8080", nil))
}

The example is defining the main function using the keyword  func. Go has strict rules about where the opening curly brace should be, so make sure the starting brace is on the correct line. The first statement in main defines that all web requests to the root path (“/”) will be handled by the index, a function of type  http.HandlerFunc.

The second line starts the web server through the  http.ListenAndServe function. It signals the server to continuously listen for incoming HTTP requests on the server’s port 8080. The second parameter of this function is needed to block the program until the end.

Since  http.ListenAndServe  always returns an error, the example will wrap this call inside the  log.Fatal call. This statement logs any error messages generated on the server side.

Implement the handling function

As you can see, the main function calls the handler function index to deal with client requests. However, the example has yet to define this functionality for its server.

Let’s add the necessary statements 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, of the form http.Request object.

The  Fprintf  function from the  fmt  package is used to display and manipulate text strings. The article is using this to display the server’s response to web requests. Finally, the  r.URL.Path[1:]  element is used to fetch data after the root path.

Add all the rest

Your Go Web server will be ready once you have added all the rest. The code will look like this:

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("/", index)
log.Fatal(http.ListenAndServe(":8080", nil))
}

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

How to build a basic web server using Go

Compile this Go web server code as an executable

How to automatically turn on Linux

You may also like

1 comment

How to set up a local web server (Local Web Server) on Windows, macOS and Linux - TotalDrivers.net July 26, 2021 - 11:13 am

[…] How to build a basic web server using Go […]

Reply

Leave a Comment