Photo by Pixabay: https://www.pexels.com/photo/computer-coding-270632/

Introduction

I have been experimenting with Go for a while now, and I am beginning to like it more and more. Especially the way Go handles concurrency makes code clear, readable, and therefore more secure and maintainable.

Go, as I understand it, is also extensively used in the development of web-applications, especially API’s, and with the advent of frameworks like Gin-gonic and Fibre, it is not hard to understand why.

That is why I embarked on a project to build a simple website with HTMX, which is a new technology for me, but which sounds very exciting, and combine it with a backend written in Go.

Normally speaking I would write my API to produce JSON of some sort. I will not do this in this example, the API will produce HTML. In a way I am doing SSR (Server Side Rendering)

But before we can get to all of this, let’s set everything up.

Prerequisites

In order to follow along you do need to following things:

  • A working knowledge of the Go language.
  • A working Go compiler on your machine.
  • An IDE or a text editor. If you want something for free, you can use Visual Studio Code, but if you want spend some money, though not much, you can have a look Jetbrains’ Goland
  • Since we are storing our items in a real database, you will need access to an instance of Postgresql, either locally or through some service.
  • If you are going to use a database, pgAdmin as the admin tool will come in quite handy.

Start the setup

On your computer open a terminal or a commandline in an empty directory, basically the directory where you want to build this project and type:

go mod init <your github username without spaces>/todolistproject

If you do not have a username, use your own name without spaces. This will initialize the go.mod file, which will hold all of your dependencies.

The gin package

Since we are going to build a web api, we need some form of web-framework. Although there are many to choose from, for this project I chose gin-gonic. To install this issue this command:

go get -u github.com/gin-gonic/gin

This will install the gin-gonic webframework along with all of its dependencies.

The GORM package

If we want to talk to a database in our Go applications, the best way to do it is some ORM, an Object Relational Mapper. This piece of software translates our objects and queries on our objects to queries on our database. Even better that that, many ORM’s, and GORM does that as well, can keep our object-model and our database synchronized through the use of so-called migrations as we will see later.

To install GORM we first install the base-package:

go get -u gorm.io/gorm

But since we are also using Postgresql, we also need to install the driver for postgres:

go get -u gorm.io/driver/postgres

Well, this wasn;t so hard. In part 2 we will build our model, and the code to access the database.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *