Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/saro0307/calculator-using-golang
functionalities of a calculator represented using Golang
https://github.com/saro0307/calculator-using-golang
calculator calculator-app calculator-application golang golang-application golang-package
Last synced: about 2 months ago
JSON representation
functionalities of a calculator represented using Golang
- Host: GitHub
- URL: https://github.com/saro0307/calculator-using-golang
- Owner: saro0307
- License: mit
- Created: 2024-06-18T16:26:24.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-07-05T13:39:44.000Z (6 months ago)
- Last Synced: 2024-07-09T08:40:35.702Z (6 months ago)
- Topics: calculator, calculator-app, calculator-application, golang, golang-application, golang-package
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# README
## Calculator Using Golang
This project is a simple HTTP server written in Go that performs basic arithmetic operations (addition, subtraction, multiplication, and division) on two numbers provided in a JSON request. The server listens on port 8090.
### Prerequisites
- Go 1.13 or higher
### Installation
1. Clone the repository:
```sh
git clone https://github.com/saro0307/calculator-using-golang.git
```
2. Navigate to the project directory:
```sh
cd calculator-using-golang
```### Usage
1. Build and run the server:
```sh
go run main.go
```
The server will start listening on `http://localhost:8090`.2. Send a POST request with a JSON payload to the server:
```sh
curl -X POST http://localhost:8090/ -d '{"num1": 10, "num2": 5}' -H "Content-Type: application/json"
```### Example
**Request:**
```json
{
"num1": 10,
"num2": 5
}
```**Response:**
```json
{
"add": 15,
"mul": 50,
"sub": 5,
"div": 2
}
```### API Endpoint
- **`POST /`**
- **Request Body:** JSON object with the fields `num1` and `num2`, both of which are float64.
- **Response Body:** JSON object with the results of addition (`add`), multiplication (`mul`), subtraction (`sub`), and division (`div`).### Code Explanation
1. **Structs:**
- `numbers`: Represents the input JSON object with `num1` and `num2` as fields.
- `numsResponseData`: Represents the response JSON object with the results of the arithmetic operations.2. **Functions:**
- `process(numsdata numbers) numsResponseData`: Takes a `numbers` object as input and returns a `numsResponseData` object with the results of the arithmetic operations.
- `calc(w http.ResponseWriter, request *http.Request)`: HTTP handler function that decodes the JSON request, processes the numbers, and encodes the JSON response.3. **Main Function:**
- `main()`: Sets up the HTTP server to handle requests at the root endpoint (`"/"`) using the `calc` function.### Error Handling
The current implementation does not include extensive error handling. It is recommended to add checks for division by zero and ensure valid JSON payloads in production code.
### License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
### Acknowledgements
This simple arithmetic server was created as a learning project and is intended for educational purposes.
---
Feel free to contribute, report issues, or fork this repository for your own use!