A RESTful API for user management built with Go (Golang) using Clean Architecture and MySQL as the database.
| Technology | Description |
|---|---|
| Go | Backend language |
| net/http | HTTP server (native) |
| MySQL | Database |
| godotenv | Load environment variables |
| bcrypt | Password hashing |
restfull-api-go/
βββ cmd/api/main.go # Entry point
βββ config/config.go # Configuration
βββ internal/
β βββ domain/user.go # Entity & interface
β βββ repository/user_repository.go # Database layer
β βββ usecase/user_usecase.go # Business logic
β βββ delivery/http/
β βββ handler/user_handler.go # HTTP handlers
β βββ middleware/logger.go # Logger middleware
β βββ router/
β βββ router.go # Main router
β βββ user_router.go # User routes
βββ pkg/
β βββ database/mysql.go # DB connection
β βββ helper/response.go # Response helper
βββ .env.example # Environment template
βββ Makefile # Build scripts
βββ go.mod
Make sure the following tools are installed on your machine:
| Tool | Version | Link |
|---|---|---|
| Go | >= 1.22 | golang.org/dl |
| MySQL | >= 8.0 | mysql.com |
| Make | any | Available by default on Linux/macOS |
| Git | any | git-scm.com |
Check your Go version:
go version
# Expected output: go version go1.22.x ...git clone https://github.com/VoinzzZ/restfull-api-go.git
cd restfull-api-gogo mod tidy# Copy the environment template
cp .env.example .env
# Edit .env with your database configuration
nano .env # or use your preferred editorFill in your .env file:
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=go_api_db
PORT=8080Log in to MySQL and run:
CREATE DATABASE go_api_db;
USE go_api_db;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);make runIf successful, the terminal will display:
2026/02/19 01:12:00 Database connected successfully
2026/02/19 01:12:00 Server starting on http://localhost:8080
Base URL: http://localhost:8080
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/v1/users |
Create a new user |
GET |
/api/v1/users |
Get all users |
GET |
/api/v1/users/{id} |
Get user by ID |
PUT |
/api/v1/users/{id} |
Update user |
DELETE |
/api/v1/users/{id} |
Delete user |
POST /api/v1/users
Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}Response (201 Created):
{
"status": 201,
"message": "User created successfully",
"data": {
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2026-02-19T01:12:00Z",
"updated_at": "2026-02-19T01:12:00Z"
}
}GET /api/v1/users
Response (200 OK):
{
"status": 200,
"message": "Users retrieved successfully",
"data": [
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2026-02-19T01:12:00Z",
"updated_at": "2026-02-19T01:12:00Z"
}
]
}GET /api/v1/users/{id}
Response (200 OK):
{
"status": 200,
"message": "User found",
"data": {
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2026-02-19T01:12:00Z",
"updated_at": "2026-02-19T01:12:00Z"
}
}Response (404 Not Found):
{
"status": 404,
"message": "user not found"
}PUT /api/v1/users/{id}
Request Body:
{
"name": "John Updated",
"email": "john.new@example.com"
}Response (200 OK):
{
"status": 200,
"message": "User updated successfully"
}DELETE /api/v1/users/{id}
Response (200 OK):
{
"status": 200,
"message": "User deleted successfully"
}# Create user
curl -X POST http://localhost:8080/api/v1/users \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com","password":"password123"}'
# Get all users
curl http://localhost:8080/api/v1/users
# Get user by ID
curl http://localhost:8080/api/v1/users/1
# Update user
curl -X PUT http://localhost:8080/api/v1/users/1 \
-H "Content-Type: application/json" \
-d '{"name":"John Updated","email":"john.new@example.com"}'
# Delete user
curl -X DELETE http://localhost:8080/api/v1/users/1make run # Run the server (development)
make build # Compile to binary
make start # Run the compiled binary
make clean # Remove binaryThis project uses Clean Architecture with the following flow:
Request β Router β Middleware β Handler β Usecase β Repository β Database
β
Response β Router β Middleware β Handler β Usecase β Repository β Database
| Layer | Package | Responsibility |
|---|---|---|
| Domain | internal/domain |
Entity & interface contract |
| Repository | internal/repository |
Database operations |
| Usecase | internal/usecase |
Business logic |
| Handler | internal/delivery/http/handler |
HTTP request/response |
| Middleware | internal/delivery/http/middleware |
Cross-cutting concerns |
| Router | internal/delivery/http/router |
Route definitions |
Contributions are welcome! Feel free to open an issue or submit a pull request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request