Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Go RESTful API

Go Version PRs Welcome Maintained MySQL

A RESTful API for user management built with Go (Golang) using Clean Architecture and MySQL as the database.


πŸ› οΈ Tech Stack

Technology Description
Go Backend language
net/http HTTP server (native)
MySQL Database
godotenv Load environment variables
bcrypt Password hashing

πŸ“ Project Structure

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

βš™οΈ Requirements

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 ...

πŸš€ Quick Start

1. Clone the repository

git clone https://github.com/VoinzzZ/restfull-api-go.git
cd restfull-api-go

2. Install dependencies

go mod tidy

3. Setup environment

# Copy the environment template
cp .env.example .env

# Edit .env with your database configuration
nano .env   # or use your preferred editor

Fill in your .env file:

DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=go_api_db
PORT=8080

4. Setup database

Log 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
);

5. Run the server

make run

If 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

πŸ“‘ API Endpoints

Base URL: http://localhost:8080

Users

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

πŸ“‹ API Reference

Create 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 All Users

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 User by ID

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"
}

Update User

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 User

DELETE /api/v1/users/{id}

Response (200 OK):

{
  "status": 200,
  "message": "User deleted successfully"
}

πŸ§ͺ Testing with cURL

# 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/1

πŸ“¦ Makefile Commands

make run    # Run the server (development)
make build  # Compile to binary
make start  # Run the compiled binary
make clean  # Remove binary

πŸ—οΈ Architecture

This 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

🀝 Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

About

Production-ready RESTful API for User Management with Go

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages