Should You make Dynamic Websites in Golang?

Is Golang an optimal platform for making simple websites?

Jake Mellichamp
Nerd For Tech

--

Introduction

This past month I decided to branch out and learn a new skill. I wanted to recreate an outdated website and host it on the cloud, it sounded like a great opportunity to learn Golang while also doing something cool!

To clear the air, yes! It is 100% feasible to create websites in Golang, the website created can be located here if you want to take a look. The question I want to address rather, is: “is creating a website using Go worth your time?”

The Primary Use cases for Golang.

Go is a relatively new programming language that became an open-source project in 2009. Some of the top companies that use Golang are Google, Uber, and Twitch. As to the documentation on its official website:

“Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of networked machines”

A list of some of the use cases can be seen below:

  • Advanced performance for networking and the full use of multi-core machines
  • Go is a compiled language, and gives Go a speed performance similar to that of C and C++.
  • The syntax is relatively easy to pick up if you are familiar with C, C++, or other OOP languages.

So full disclosure, whatever comes next is only my opinion!

The Project: A Dynamic Website

A 5 part series of developing a website in Go! Made by yours truly.

The goal of the project was to create a personal website that could showcase some relatively basic information about myself, send SMTPS emails via post request, and dynamically showcase previously written blogs! I normally write websites in python’s Django and Flask frameworks so you’ll notice me back comparisons using those frameworks!

Con: The Development Process

My first takeaway with Golang was that there was no Live Reloading natively with the language.

Figure 1.1 Example of Live Reload in Django

Live Reloading allows changes to be made to html, css, and source files without having to recompile the application. This is extremely useful in web development as you can imagine, “ahhh… if only that image was a little bit more to the right…” As my frustration brewed, I took to Google and searched “live reloading in Golang” and behold! Some people developers actually made a package called air that will handle that functionality for you!

Figure 1.2 Using air in Golang

As you can see, the code air run .\main.go runs our web server, and as changes are made in the repository, the application auto rebuilds and deploys. So after the installation of some third-party packages, Golang can be configured to use Live reloading.

Pro: Handling Get vs Post Request

The handling of HTTP requests in go was a fairly pleasant experience. On the webserver there are handler functions that handle any web request that point to a given URL.

Figure 2.1 Interpreting Get and Post Request to the contact page

In the example code above, our Handler is listening to the address “127.0.0.1:80/contact" and with the use of the http.Request.Method field, we can utilize a switch statement to determine which request is coming in. Super easy!

Con: HTML Template Inheritance

This was probably one of the most disappointing things I experienced while programming in go, the lack of HTML template inheritance that could be utilized.

Figure 3.1 Template Inheritance in Django

In the web frameworks I am familiar with, one can usually utilize template inheritance. This is quasi-polymorphism but for HTML files. Essentially, instead of having one large HTML file for each of your web pages, you can have many html files that tie together to make an individual webpage. This helps with organizing your html code.

Golang on the other hand, only supports one level of template inheritance.

Figure 3.2 Template Inheritance in Golang

Why is this? Well, the language relies heavily on structs. When executing a webpage in Golang, you can pass a data struct along with it to the webpage. The HTML data passed is of type string which means you can inject a section of hardcoded HTML into a webpage. However, if the data you want to inject is dynamic (lets say, another webpage that contains a dynamic list of Blogs) then that data will be interpreted literally, not dynamically. This is an example of working code in Golang:

Figure 3.3 Base.html being passed data from main.go file

As you can see, we have a data object of type PageData which contains a couple of fields to display on the base.html webpage. When the webpage is executed in the GET method, you can access the variable passed via:

{{ .<field name> }} 

These variables were underlined in yellow for your convince! Depending on your use case, this might be enough functionality for you in your quest of web development.

Conclusion

Golang all in all is pretty fun to program in, I definitely enjoyed the way it handled its networking and socket functionality compared to other languages. The syntax wasn’t too hard to pick up, and I definitely plan on utilizing it more often to make microservices in the future.

That being said, each language/technology is a tool in the Software Engineers Tool Belt. If I could go back in time, I would not recommend using Golang for simple website development.

In reality, it was like trying to drive a nail into wood using a spoon instead of a hammer. Will it work? Absolutely, but it might take you a little more effort. If you want to make a simple static or dynamic website my go-to option in the future will still be Django.

Leave a comment on what you guys think of Golang, and how you utilize it — or don’t agree with my take? Let me know why!

--

--