Learning GO: Obtaining a unique sequence from #PostgresSQL with #golang

By | May 17, 2018

At some point I discovered GO trying to do very simple and fast code for specific tasks without having to load lots of libraries, deploy application servers or set up complicated frameworks. If you want to build fast very small native micro-services that can be deployed in a docker instance < 10MB Go is the king.

Due to the huge number of standard libraries and libraries from go community in go everything is really concise and with limited number of code lines.
The following is an example on how to connect to a PostgreSQL database and obtain a unique sequence number. This is very useful when you need to assign unique ids to some entry.

STEP 1: Prerequisites
We assume that we already have a PostgreSQL setup on our system. The easy way to do it just pull it out of the Docker repo.
In a docker-compose.yml just define a db service as:


After the database container is created and started just connect to it:

Start the PostgreSQL cli:

Connect to our defined db mydb:

Simply execute the following query to create the sequence table:

STEP 2: Go import section, register the database driver
Next step is to simply create a package for our db functions and import some basic packages

What is important here is the import:
_ “github.com/lib/pq”

Note the _ before the import. In the case of github.com/lib/pq, the underscore import is used for the side-effect of registering the pq driver as a database driver in the init() function, without importing any other functions.

STEP 3: Define the sequence function

We can now define a sequence function that uses the above define db table to store unique ids. For example unique ids for each book of a user.

STEP 4: Build connection string and open a connection to the database
To open a connection to the database is very easy with the following code block:

Note that we use some global variables we defined in the init() function of the package from some environment variables (DbHost, DbPort, DbUser, DbPassword, DbName). It is always better and more secure to get them as global parameters.

Note how simple we make sure we close the connection: defer db.Close(). By using the “defer” keyword we instruct go to execute some code line at the moment when we exit the function. This is extremely useful for a programmer as the majority of resource leaks are caused by forgotten resources that are not properly close. By being able to close a socket connection, a stream or a buffer right after you opened it is very helpful in resource management.

STEP 5: Create the sequence date field
Here I want to record the date when the entry was added. I record just the month and day.

STEP 6: Insert a new entry and return the sequence:
now simply insert a new entry and concatenate the input params to the returned unique id:

STEP 7: Putting all together

STEP 8: Use the sequence:

To use the sequence in some other go code just import the package if we are not in the same package and call the function.

The returned sequence is something like: ScienceBook-42-1001

where user_id is some unique id identifying the user connected to the system.

This is by far the best way to create a sequence or the best choice of parameters. I only wanted to show how easy is to interact from go with a database.

Contribute to this site maintenance !

This is a self hosted site, on own hardware and Internet connection. The old, down to earth way 🙂. If you think that you found something useful here please contribute. Choose the form below (default 1 EUR) or donate using Bitcoin (default 0.0001 BTC) using the QR code. Thank you !



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.