A simple RESTful API for managing book data using Golang, GORM, MySQL, and Gorilla Mux.
- ✅ Get all books
- ✅ Get a book by ID
- ✅ Create a new book
- ✅ Update a book by ID
- ✅ Delete a book by ID
- Gorilla Mux v1.8.1 – HTTP router and URL matcher
- Google UUID v1.6.0 – Generate and parse UUIDs
- GORM v1.30.0 – ORM library for Golang
- GORM MySQL Driver v1.5.7 – GORM dialect for MySQL
- Go SQL Driver for MySQL v1.9.2 – Native MySQL driver for Go
- Joho Godotenv v1.5.1 – Load environment variables from
.env
go-restful-api/
├── config/
│ └── db.go
├── handlers/
│ └── book_handler.go
├── models/
│ └── book.go
├── routes/
│ └── routes.go
├── .env
├── go.mod
├── LICENSE
├── main.go
└── README.md
git clone https://github.com/fillahalfatih/go-restful-api.git
cd go-restful-api
Create a .env
file and fill in your database credentials:
DB_USER=root
DB_PASSWORD=
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=db_name
go mod tidy
go run main.go
The server will run at:
http://localhost:8080
Method | Endpoint | Description |
---|---|---|
GET | /api/books | Get all books |
GET | /api/books/{id} | Get a book by ID |
POST | /api/books | Add a new book |
PUT | /api/books/{id} | Update a book by ID |
DELETE | /api/books/{id} | Delete a book by ID |
{
"id": "a07e28fc-4b4f-4bf8-8fd7-72f9bd1235fc",
"isbn": "448-1234567890",
"title": "Book One",
"year": 2025,
"author_id": 2,
"author": {
"id": 2,
"first_name": "Grant",
"last_name": "Gustin"
}
}
Free to use for learning and personal exploration.