Build a webserver in Go with Handle and HandleFunc

Build a webserver in Go with Handle and HandleFunc

In this article let's make our first resource server. Most of what we need will be provided by Go in "net/http" package of the standard library.
There are three objects that are going to form the core of our work.

The http.ListenAndServe() function is used to kick off the process that allows the server to begin to listen and respond to HTTP requests. When called, this function will block the main thread so it is important to call this after all configuration is complete. When the application is running, Go will handle each request by using what are called goroutines. These are lightweight threadlike constructs that Go uses to support concurrent code execution, such as listening for HTTP requests, while also responding to requests that it has already received.

The http.Handle() function allows a URL path to be handled by an object that implements the HTTP handler interface. This can be used when complex logic is happening to handle the request.

The http.HandleFunc() function - other major way to register listener on a request path. In this case we register a function to handle the request instead of an object. This can be used when the logic to handle the request is easier.

A very simple example: goo.png

The http.HandleFunc() function takes two arguments: the pattern that it will be match, and after that the handler function. The function in this case in an anonymous function with two parameters: the first is normally called w which has a type http.ResponseWriter, and the second normally called req is a pointer to an object that represents information about that Go has gathered about the request itself. In the handler function we are simply going to write a string out to the responseWriter's write method. Since the w.Write() method takes []byte type data, we need to cast it explicitly to that type.

After that we implement the code that allow us to start listening for requests. This is the http.ListenAndServe() method which this time takes two arguments: the first the address we want our application to listen on, and for the second argument we pass nil to tell Go that we want to use the default server multiplexer or MUX to handle the requests.

Some words about this MUX

So when a request comes into the application Go receives that in the primary thread. It will then spin up a lightweight thread called a goroutine that runs concurrently to the primary thread. It then delegates the responsibility of handling the request to that goroutine. These technique of managing multiple requests at the same time is called multiplexing - MUX. There are scenarios when the default mux is unefficient. These times you can create your own server.

Make an own handler for the server calls

If you would like to use own handler functions check out the picture below

go2.png

If you want to write an own handler, you must make a function that implements the http.Handler interface as you can see in line 23. After this you have to write your handler function to determine what you would like to do (line 27), if the request appears. In this case we want to provide files frome the "public" folder, for example an index.html static file.

Thank you for reading this article, I hope it was helpful for you.

Did you find this article valuable?

Support Renátó Bogár by becoming a sponsor. Any amount is appreciated!