Welcome to the Simple Go Calculator repository! This project provides a straightforward calculator implementation in Go, allowing you to perform basic arithmetic operations either in a console mode or via a server mode. Choose the mode that best suits your needs!
доступно на русском
Console Mode: Run the calculator directly in your terminal for quick calculations.
Server Mode: Start a lightweight HTTP server to handle calculations via API requests.
Supports basic arithmetic operations: +
, -
, *
, /
.
Easy to use and extend.
graph TD
U[User] -->|POST /calculate| O(Orchestrator)
U -->|GET /expressions| O
O -->|GET /internal/task| A1([Agent 1])
O -->|GET /internal/task| A2([Agent 2])
O -->|GET /internal/task| A3([Agent 3])
A1 -->|POST /internal/task| O
A2 -->|POST /internal/task| O
A3 -->|POST /internal/task| O
To get started, make sure you have Go installed on your machine. You can download it from here.
Clone the repository:
git clone https://github.com/shzuzu/Go_Calculator.git
cd Go_Calculator
Install dependencies:
go mod tidy
- Console Mode Run the calculator in your terminal for interactive calculations:
go run ./cmd/main.go --mode=console
Enter your arithmetic expressions directly in the terminal. Example:
Input expression (enter "exit" to exit):
2+2*2
2+2*2 = 6
- Server Mode Start the calculator as an HTTP server to handle calculations via API:
go run ./cmd/main.go --mode=server
The server will start on http://localhost:8080/
.
Send a POST request with your expression to /api/v1/calculate
:
curl --location 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
"expression": "2+2*2"
}'
Example response 1:
{
"id": 1
}
Example request 2 /extensions/{id}
:
curl localhost:8080/api/v1/expressions/1
Example response 2:
{
"id": "1",
"status": "done",
"result": 6
}
- Several requests
curl --location 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
"expression": "2+2*1"
}'
curl localhost:8080/api/v1/expressions
- Responce:
{
"expressions": [
{
"id": "1",
"status": "done",
"result": 6
},
{
"id": "2",
"status": "done",
"result": 4
}
]
}
The server handles various error scenarios gracefully and returns appropriate HTTP status codes and messages. Below are the details of the errors you might encounter:
❌422 Unprocessable Entity
This error occurs when the provided expression is syntactically correct but cannot be processed.
Example request 1:
curl --location 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
"expression": "313 / 0"
}'
Example responce 1:
{
"error": "Division by zero"
}
Example request 2:
curl localhost:8080/api/v1/expressions/1
Example responce 2:
{
"id": "1",
"status": "error",
"result": null
}
❌500 Internal Server Error
This error occurs when there is an unexpected issue on the server side, such as a bug in the code or an unhandled exception.
Example Response:
{
"error": "Internal server error"
}
❌405 Method Not Allowed
This error occurs when an unsupported HTTP method is used for a route. For example, using GET instead of POST for the /api/v1/calculate
endpoint.
Example Request:
curl -X GET http://localhost:8080/calculate -d '{"expression": "2 + 2"}'
Example Response:
{
"error": "Method not allowed"
}
The project includes unit tests to ensure the calculator works as expected. Run the tests using:
go test ./internal/application
go test ./pkg/calc
This project is licensed under the MIT License. See the LICENSE file for details.
If you have any questions, suggestions, or issues, feel free to open an issue or reach out to me directly.
Happy calculating! 🎉
This is the end, fren..
BTW, THE CAT ↓
_ |\_
\` ..\
__,.-" =__Y=
." )
_ / , \/\_
((____| )_-\ \_-`
`-----'`-----` `--`