REST API example using Go, Gin, GORM, and PostgreSQL.
- Go 1.22
- Gin — HTTP framework
- GORM + pgx — ORM & PostgreSQL driver
- go-playground/validator — request validation
- zerolog — structured logging
- Go 1.22+
- PostgreSQL
- Clone the repository and install dependencies:
go mod tidy- Copy the environment file and fill in your database credentials:
cp .env.example .envDB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=golang-crud- Create the database:
CREATE DATABASE "golang-crud";make runOr without Make:
go run main.goThe server starts on http://localhost:8888.
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/tags |
Get all tags |
| GET | /api/tags/:tagId |
Get tag by ID |
| POST | /api/tags |
Create a tag |
| PATCH | /api/tags/:tagId |
Update a tag |
| DELETE | /api/tags/:tagId |
Delete a tag |
POST /api/tags
// Request
{ "name": "golang" }
// Response 200
{ "code": 200, "status": "Ok" }GET /api/tags
// Response 200
{
"code": 200,
"status": "Ok",
"data": [
{ "id": 1, "name": "golang" }
]
}GET /api/tags/:tagId
// Response 200
{ "code": 200, "status": "Ok", "data": { "id": 1, "name": "golang" } }
// Response 404
{ "code": 404, "status": "Not Found" }make test # run all tests
make build # build binary to bin/app
make lint # run go vet
make tidy # go mod tidy
make clean # remove build artifacts.
├── config/ # database connection
├── controller/ # HTTP handlers
├── data/
│ ├── request/ # request DTOs
│ └── response/ # response DTOs
├── helper/ # shared utilities and sentinel errors
├──
5068
model/ # GORM models
├── repository/ # data access layer
├── router/ # route definitions
└── service/ # business logic