In this article, we will learn about implementing CRUD in Golang REST API with Echo for routing requests, GORM as the ORM to access the database, Viper for Loading configurations, and PostgreSQL as the database provider. We will also follow some clean development practices to help organize the GoLang project folder structure in a more easy-to-understand fashion.
- Setting up the Golang Project
- Loading Configurations using Viper
- Defining the Product Entity
- Connecting to the database
- Routing
- Implementing CRUD in Golang Rest API
- Create
- Get By ID
- Get All
- Update
- Delete
- Testing CRUD Operations
Read the entire article here - https://codewithmukesh.com/blog/implementing-crud-in-golang-rest-api/
- Create your own database on PostgreSQL Server
- Rename config.json.example to config.json
- Change the value of 'connection_string' in the config file:
{
"connection_string": "user=username password=yourpassword dbname=yourdb host=localhost port=5432 sslmode=disable TimeZone=Asia/Singapore",
"domain": "http://localhost",
"port": 8080
}
go get -u
go get
go run .
go build -o file_name
./file_name
Install plugin on vscode named: REST Client
or others like 'ext:rest'
Test the rest API at folder:
~/golang-crud-rest-api/collection/products.rest