While it’s a no-brainer, Golang has impressive features that support the needs of microservice architecture, large-scale enterprise applications, and distributed systems.
Nevertheless, Golang was built with web development in mind and had sophisticated web packages that are part of its core library to help developers build high performant web applications.
The Golang community is rapidly growing. Many developers are already using Golang for full-stack development and mastering new modules and frameworks that make Golang a vital language for web development.
As of Golang 1.15 release, the core library has been updated with improvements to encoding/json, net/http, database/sql, and database/sql/driver.
This article will guide you on how to set up a Golang project with the GORM library and PostgreSQL to build CRUD RESTful API to perform the basic Create/Get/Update/Delete operations.
Prerequisites (Golang, PostgreSQL, Docker)
Before going further with this tutorial, you will need:
- A development environment running Golang. Click here to download the latest version of Go.
- Familiarity with Golang, SQL, and PostgreSQL queries will be highly beneficial.
- Have Docker installed on your machine.
- Have PostgreSQL installed. This is optional since we will use Docker to run the Postgres server.
What is an ORM?
Object-Relational Mapping (ORM) is a technique that enables developers to use their native programming paradigm to query and manipulate data from a database.
In layman’s terms, an ORM provides a data mapper pattern to transform data defined in classes or objects to SQL. ORMs also come with CRUD functions already implemented, making it easier for developers to perform CRUD operations against the database.
Luckily for us, the Golang community has built several developer-friendly Object Relational Mapping libraries to allow Golang developers to use JSON
key:value pair syntax and encoding to map directly to any SQL database like SQLite, MySQL, PostgreSQL, and many more.
Step 1 – Setup the Golang Project
In this step, you will initialize a new Golang project, create a PostgreSQL Docker server, and validate the environment variables with the Golang Viper package.