Web Application Basics in Go

Go has some great support for easily building web applications utilising the supplied http package. There are however some concepts to grasp before fully understanding how Go handles web requests which you should probably know before creating your own applications.

The first thing to understand is how Go handles incoming HTTP requests. There are two main concepts to understand here:

ServeMux

What is ServeMux? It sounds like a complicated name but it really boils down to how Go handles input and output in the form of HTTP requests and responses.

The Mux part of ServeMux stands for multiplexer. A multiplexer is really just a device that takes multiple inputs signals, does some processing and generates an output signal. This is analogous to a web server that takes multiple input requests (GET /users, POST /users etc) does some processing, for example gets some data from a database and generates an output in the form of a response (JSON, HTML etc).

The next concept to grasp is how Go handles connections:

Handling connections

The server listens on a specific port, say port 80. By default the DefaultServeMux within the http package will handle all the connections and messages going back and forward. DefaultServeMux will create Goroutines to handle each connection which allows the server to be highly concurrent and performant.

What are goroutines then?

Essentially you can think of goroutines as analogous to threads inside the operating system. They are Go’s way of handling asynchronous processing and concurrency.

The go keyword here tells go to execute the doSomething function as a goroutine concurrently with the calling code.

Creating a basic web service

So now we understand the basic concepts above we can go ahead and create an actual web service.

There are two main functions from the http package that we need to work with.

The first is the http.Handle function.

The Handle function allows us to define a handler interface and attach a function to that interface. We can then tell Go to execute that function when a specific web requests is made. An example below defines the myHander interface, which the ServeHTTP function attaches to. When we tell Go to handle the request /custom, we pass the function call to Handle with our interface definition populated.

There is a less verbose way of achieving the same thing using the http.HandleFunc method in the Go http package.

Instead of having to define our own handler interface we can use the built in handler interface func(ResponseWriter, *Request) and then pass our implantation of that interface to HandleFunc as shown below

And that’s it! A very basic overview of creating a simple web service in Go using the http package.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lee Dale

Lee Dale

I am a lead software developer/cloud architect who has been designing and building software solutions professionally for the last twenty years.