A RESTful API service built with Go and PostgreSQL for managing product data. This project demonstrates best practices for structuring a Go application with database integration and HTTP endpoints.
go-postgres/
├── cmd/
│ └── server/
│ └── main.go # Application entry point
├── internal/
│ ├── database/
│ │ └── postgres.go # PostgreSQL implementation
│ ├── models/
│ │ └── product.go # Data models
│ └── handlers/
│ └── product_handler.go # HTTP handlers
├── pkg/
│ └── store/
│ └── store.go # Store interface
├── .env # Environment variables
├── go.mod # Go module file
└── README.md # This file
- Go 1.21 or later
- PostgreSQL 12 or later
- Git
- Clone the repository:
git clone https://github.com/Mohabdo21/go-postgres.git
cd go-postgres- Install dependencies:
go mod tidy- Create a
.envfile in the root directory:
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=your_database
DB_SSL_MODE=disable
SERVER_PORT=8080- Create a PostgreSQL database:
CREATE DATABASE your_database;- The application will automatically create the required
productstable on startup.
- Start the server:
cd cmd/server
go run main.go- The server will start on the configured port (default: 8080)
POST /products
Request Body:
{
"name": "Apple",
"price": 0.50,
"available": true
}
Response:
{
"id": 1,
"name": "Apple",
"price": 0.50,
"available": true,
"created": "2024-11-25T10:00:00Z"
}GET /products
Response:
[
{
"id": 1,
"name": "Apple",
"price": 0.50,
"available": true,
"created": "2024-11-25T10:00:00Z"
}
]The store.Store interface in pkg/store/store.go defines the contract for database operations:
type Store interface {
CreateProduct(ctx context.Context, product *models.Product) error
GetProducts(ctx context.Context) ([]models.Product, error)
}The PostgreSQL implementation in internal/database/postgres.go provides:
- Connection pooling
- Prepared statements
- Error handling
- Transaction support
The handlers in internal/handlers/product_handler.go provide:
- Request validation
- JSON serialization/deserialization
- Error handling
- HTTP status codes
The application can be configured using environment variables:
| Variable | Description | Default |
|---|---|---|
| DB_HOST | PostgreSQL host | localhost |
| DB_PORT | PostgreSQL port | 5432 |
| DB_USER | Database username | - |
| DB_PASSWORD | Database password | - |
| DB_NAME | Database name | - |
| DB_SSL_MODE | SSL mode for database connection | disable |
| SERVER_PORT | HTTP server port | 8080 |
- Define new methods in the
Storeinterface - Implement the methods in
PostgresStore - Create new handlers in
product_handler.go - Register the new routes in
main.go
The project follows standard Go coding conventions:
- Use
gofmtfor code formatting - Follow Effective Go guidelines
- Use meaningful package and variable names
- Add comments for exported functions and types
The application implements comprehensive error handling:
- Database errors are wrapped with context
- HTTP errors include appropriate status codes
- Validation errors provide clear messages
- Connection pooling is configured for optimal database performance
- Context timeouts prevent hanging operations
- Prepared statements reduce query parsing overhead