This is a simple API built with Go (Golang) that classifies a given number and provides various properties, including:
- Whether the number is prime
- Whether the number is perfect
- Whether the number is an Armstrong number
- Whether the number is even or odd
- The sum of its digits
- A fun fact fetched from an external API
- Provides multiple classifications for a number.
- Fetches a fun fact about the number from the Numbers API.
- Returns data in JSON format.
- Handles errors gracefully (e.g., invalid input).
- Go 1.16 or higher.
- An internet connection to access the Numbers API for fun facts.
-
Clone the repository:
git clone https://github.com/yourusername/number-classifier-api.git
-
Navigate into the project directory:
cd number-classifier-api
- Initialize Go modules (if you haven't already):
go mod init number-classifier-api
- Install dependencies:
go mod tidy
- Running the API Run the server using the following command:
go run main.go
The server will start and listen on port 8080 by default.
You can access the API by sending requests to:
http://localhost:8080/api/classify-number?number=<your-number>
For example, to classify the number 371:
http://localhost:8080/api/classify-number?number=371
GET /api/classify-number?number=371
{ "number": 371, "is_prime": false, "is_perfect": false, "properties": ["armstrong", "odd"], "digit_sum": 11, "fun_fact": "371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371" }
If an invalid input is provided (e.g., a string instead of a number): { "number": "alphabet", "error": true }