Unit testing Golang code

In this article I’m going to explain how to write and run unit tests for Golang code using go test. If you are completely new to the language, I recommend you take a look at my introduction to Golang article.

Packages

Testing in the Golang world revolves around packages, so we need to understand what a package is before we can understand how to test code.

A package is nothing more than a way of grouping related code. In Golang, a folder can only contain a single package. If we try to define two files in a folder belonging to different packages, the compiler will complain.

Read More

PostgreSQL user management

In my previous post I gave a brief introduction to PostgreSQL. In this post I’m going to dig deeper into user management and permissions.

Roles

PostgreSQL uses roles for authentication. There are two different kind of roles: groups and users. Users and groups can belong to groups; The only difference is that users can be used to log-in to a database. If a user is created with the INHERIT property set, it will inherit permissions from the groups it belongs to.

To see all roles that currently exist on an installation of PostgreSQL, \du can be used:

1
2
3
4
5
\du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
Read More

Change video resolution using ffmpeg

I have some videos that I recorded in very high quality and ended up being too heavy. To save some space I decided to resize them and I found this can be easily done with ffmpeg.

To install ffmpeg:

1
sudo apt install ffmpeg

Then, we just need one command to change the resolution of a video:

1
ffmpeg -i input.mp4 -vf scale=-1:720 output.mp4

The scale argument means the output video will have a width of 720px and the height will be set based on the ratio of the original video.

Read More

PostgreSQL - Connection refused

While setting up a new PostgreSQL server, I noticed that I was getting a connection refused error while trying to connect to it. After checking the firewall settings and finding everything in order, I decided to check things in the host.

I used ss to inspect connections listening on port 5432 (Default port used by PostgreSQL):

1
2
3
$ ss -tna src :5432
State       Recv-Q        Send-Q       Local Address:Port        Peer Address:Port
LISTEN        0            128          127.0.0.1:5432             0.0.0.0:*
Read More

Introduction to PostgreSQL

PostgreSQL is a relational database management system (RDBMS) similar to MySQL. I usually go for MySQL, but I’m going to be working on a project that uses PostgreSQL, so I need to get familiar with it.

Installation

Installation on an Ubuntu system is easy:

1
sudo apt install postgresql
Read More

Using service workers for caching

What’s a service worker?

A service worker is a script that can be used as a client-side proxy for network requests. When the browser makes a request, a service worker can intercept it and decide what to do with it. This article is going to focus on using service workers for client side caching of web apps.

Why use service workers?

Service workers can be used to improve the performance and user experience of an application. They can be used to cache assets that are commonly needed but don’t change often, or to provide offline functionality.

Read More

Debugging assembly with GDB

I wrote a couple of articles about assembly before, and in order to understand what I was doing, I thought it would be useful to take a look at the contents of memory and registers to confirm my understanding was correct. I looked around, and found that GDB can help with this.

I wrote an introductory article to GDB a few months ago that you can check to get the basics. This article is going to build on top of it.

Debug information

In my introduction to assembly, I used this command to assemble my program:

1
nasm -f elf64 -o example.o example.asm
Read More

Concurrent data structures

A concurrent data structure, is a data structure (e.g. list, stack) that can be used by multiple threads concurently and it will always show a consistent state to each thread.

Consistency

The definition above mentions that the data structure will always show a consistent state. To understand this, let’s analyze a data structure that is not concurrent and can end in an inconsistent state.

Let’s say we are building an application that draws a single rectangle. The dimensions of this rectangle can be modified by users around the world, they just need to visit a website and enter the new dimensions.

Read More

Pointers to members in C++

A few days ago I was looking at some code and I found something I didn’t understand. It looked something like this:

1
2
void someFunction(int SomeClass::*value) {
  ...

I had never seen and * used after ::, so I had to do some research to find what that means. It turns out that is a pointer to a class member. In this post I’m going to explain how these work and when they might be useful.

Pointers to members

Pointers to members are pretty uncommon, so it will be very rarely that you will need to use them.

Read More

Operator overloading in C++

Operators are a fundamental part of programming languages. They allow us to perform operations on operands by using a symbol. If you have ever written code, you probably know what this snippet does:

1
int a = 3 + 1;

Variable a will be initialized to 4. It is initialized to this value, because the + operator has been used to add the values of 3 and 1. The = sign, is also an operator that assigns the value of the addition to the variable a.

We use these operators without thinking too much about them, but they are just symbols that perform a certain action, like any function we could define.

It turns out we can override the behavior of an existing operator. That’s what this article will focus on.

Read More