|
| 1 | +--- |
| 2 | +title: Clean Code |
| 3 | +keywords: [clean, code, fiber, postgres, go] |
| 4 | +description: Implementing clean code in Go. |
| 5 | +--- |
| 6 | + |
| 7 | +# Clean Code Example |
| 8 | + |
| 9 | +[](https://github.com/gofiber/recipes/tree/master/clean-code) [](https://stackblitz.com/github/gofiber/recipes/tree/master/clean-code) |
| 10 | + |
| 11 | +This is an example of a RESTful API built using the Fiber framework (https://gofiber.io/) and PostgreSQL as the database. |
| 12 | + |
| 13 | +## Description of Clean Code |
| 14 | + |
| 15 | +Clean code is a philosophy and set of practices aimed at writing code that is easy to understand, maintain, and extend. Key principles of clean code include: |
| 16 | + |
| 17 | +- **Readability**: Code should be easy to read and understand. |
| 18 | +- **Simplicity**: Avoid unnecessary complexity. |
| 19 | +- **Consistency**: Follow consistent coding standards and conventions. |
| 20 | +- **Modularity**: Break down code into small, reusable, and independent modules. |
| 21 | +- **Testability**: Write code that is easy to test. |
| 22 | + |
| 23 | +This Fiber app is a good example of clean code because: |
| 24 | + |
| 25 | +- **Modular Structure**: The code is organized into distinct modules, making it easy to navigate and understand. |
| 26 | +- **Clear Separation of Concerns**: Different parts of the application (e.g., routes, handlers, services) are clearly separated, making the codebase easier to maintain and extend. |
| 27 | +- **Error Handling**: Proper error handling is implemented to ensure the application behaves predictably. |
| 28 | + |
| 29 | +## Start |
| 30 | + |
| 31 | +1. Build and start the containers: |
| 32 | + ```sh |
| 33 | + docker compose up --build |
| 34 | + ``` |
| 35 | + |
| 36 | +1. The application should now be running and accessible at `http://localhost:3000`. |
| 37 | + |
| 38 | +## Endpoints |
| 39 | + |
| 40 | +- `GET /api/v1/books`: Retrieves a list of all books. |
| 41 | + ```sh |
| 42 | + curl -X GET http://localhost:3000/api/v1/books |
| 43 | + ``` |
| 44 | + |
| 45 | +- `POST /api/v1/books`: Adds a new book to the collection. |
| 46 | + ```sh |
| 47 | + curl -X POST http://localhost:3000/api/v1/books \ |
| 48 | + -H "Content-Type: application/json" \ |
| 49 | + -d '{"title":"Title"}' |
| 50 | + ``` |
0 commit comments