Init the main function and the demo procedure
Please consider supporting us by disabling your ad blocker 🙏
Demo procedure
Let’s start by initializing a test procedure that will perform a series of actions on the repository. After executing each function, we will check the results by writing them to the standard output or check if there was an error.
Create the app/demo.go file in the demo package we created at the beginning of the project and copy its contents.
The RunRepositoryDemo() function takes as arguments the context used in the repository methods and the repository itself. Note what type websiteRepository has. It is the website.Repository, which is our repository interface. This way, the RunRepositoryDemo() function can be used with any repository implementation, which is what we are going to do in the next steps. For now, this function does only one thing - it calls the Migrate() method to create our storage for Website objects.
The main() function
We already have the repository implementation and the first version of the demo procedure, so we can put everything together and run a test application.
- Create a new directory
classicin thecmdpackage for the application that demonstrates the classic repository based on thedatabase/sqlpackage. - Add a new file
main.goto theclassicdirectory. This is our executable file. - Copy the contents of the
cmd/classic/main.goto your newly createdmain.go. - Run our test app withIf everything went well, the program should exit without error, and a new table called
go run main.gowebsiteswith columns:id,name,url, andrankshould be created in the PostgreSQL database.
What does the main() function do?
Connect to PostgreSQL database
To connect to a PostgreSQL database using the pgx, it is necessary to register it as a database/sql driver. This is done by importing the driver package in line 11. By using a blank identifier, we import the package, even if it is not used by the current program. Once imported, it calls the internal init() function, which registers the driver in the database/sql interface under the name pgx.
The
pgxhas adatabase/sqlcompatible driver in the package github.com/jackc/pgx/v4/stdlib.
In lines 15-19, we use the registered driver. Using the sql.Open() function with the pgx as the first argument, we connect to the PostgreSQL database. The second argument is the dataSourceName consisting of the connection information. For PostgreSQL, we define the so-called connection URI here. In our case, we are connecting to the database named website located on localhost on port 5432 with username postgres and password mysecretpassword because this is what we set at the stage of running the Docker image. The result is the sql.DB object (and an error if it occurs) safe for concurrent use and maintaining its own pool of idle connections. A good practice is to close the DB connection at the end of the program with the Close() method.
Create a Website repository, context, and run the demo procedure
In line 21, we call a constructor of the PostgreSQLClassicRepository with the created sql.DB object as an argument. Then, we initialize a new Context object with a 10-second timeout using the context.WithTimeout() function. Its first argument is a parent Context which in our case is an empty fresh Context instance obtained from the Background() function.
Note that in addition to the context.Context object, as a result, we also get a cancel() function to release the resources associated with the context, so we do this at the end of the program as soon as all operations using the context have finished:
defer cancel()
In the last line of the main(), we call our demo procedure passing the created context and our classic database/sql repository as arguments.
app/demo.go
| |
cmd/classic/main.go
| |