Ilari Sorvali

Making a static website with Go, Part 1

For a while I've thought that I should have my own website. While I'm at building it, I could use some technologies I've not used before, so we get to learn something from this whole process. It would be fairly trivial to use some prebuilt static site generator (like Hugo) to make a bunch of HTML files and serve them with a webserver such as nginx or Caddy. My needs for a personal website are following:

  • Hold some basic info about myself
  • Allow me to publish posts, generated from markdown files
  • Maybe function as a cookbook, where I can post recipes so I don't always have to dig them from various places I hoard them

So it's a static website, yay. I could, in theory, just write HTML and CSS by hand (like I remember doing at elementary school IT classes) and learn about those technologies, but that's a bit shallow learning experience I guess.

I'm not a dev, certainly not a web developer, so choosing a tech stack to start with is an experience of it's own. Backend, frontend? The possibilities are almost endless, the modern internet is a wild wild west of various web development stacks. As we're making a static personal website, there's no inherent need to bring in a huge SPA web development framework like React, Svelte or Angular, and I actually don't find any interest of learning them at the moment, because they have a bit of a learning curve. (Here's a great post about SPAs that says kinda the same things that I have been unconsciously thinking)

So I want something simple and actually I've also had a thought in store about learning the Go programming language, so I looked into if I could use it at my website project. And boy did I strike gold.

Turns out Go is an excellent choice for making a website in my case, as it has a strong, but simple, standard library for handling HTTP and a powerful HTML templating package. With these tools, we can just generate the pages the site needs from the HTML templates and serve them with the built-in Go HTTP server, how neat! A one stop solution for basic web development needs, you could say.

The previously mentioned static site generator Hugo is also built with Go, and it too heavily utilizes the html/template package. This site is basically a worse server implementation of a subset of Hugos features if you think about it.

But why a server that generates and serves the HTML, why not just dump the files to disk and serve them with Caddy and be done with it? Idk, I just felt like it. It wouldn't be a massive change to make this a static site generator vs. a web server. Writing a whole server application also gives a nice look into how it can be done with just the standard library.

I also looked into Rust (as I have a budding interest in it too) for this project, but decided to go with Go as the simplicity and standard library of Go won in the end. Some project in the future will be made with Rust though, so don't worry ;)