8e790ef8ede7ba77eaa65cadfc89a8faecc199ca
Go Book API Example
This is a simple Go web service that provides a basic API for retrieving book information. It's built using the chi router and serves hardcoded book data from memory.
Project Structure
main.go: Application entry point, sets up the HTTP server and defines the/books/{id}endpoint.internal/handlers/book_handler.go: Contains theBookHandlerresponsible for processing requests to the/books/{id}endpoint. It uses an in-memory list of books.internal/models/book.go: Defines theBookstruct used to represent book data.
Getting Started
Prerequisites
- Go (version 1.16 or higher recommended)
Running the Application
-
Clone the repository:
git clone <repository_url> cd testing-go-example(Note: Replace
<repository_url>with the actual repository URL if this project were hosted.) -
Run the server:
go run main.goThe server will start on
http://localhost:3000.
API Endpoints
GET /books/{id}
Retrieves details for a single book by its ID.
Example Request:
curl http://localhost:3000/books/1
Example Response:
{
"id": "1",
"title": "The Hitchhiker's Guide to the Galaxy",
"author": "Douglas Adams"
}
curl http://localhost:3000/books/2
Example Response:
{
"id": "2",
"title": "The Restaurant at the End of the Universe",
"author": "Douglas Adams"
}
Description
Languages
Go
100%