This is a simple RESTful API built using Golang and the Gorilla Mux router. The API allows users to perform CRUD (Create, Read, Update, Delete) operations on movies. Each movie has an ID, ISBN, Title, and a Director (which contains Firstname and Lastname).
- Get all movies (
GET /movies) - Get a specific movie by ID (
GET /movie/{id}) - Create a new movie (
POST /movies) - Update an existing movie (
PUT /movie/{id}) - Delete a movie (
DELETE /movie/{id})
- Go (Golang)
- Gorilla Mux (for routing)
- Encoding/Decoding JSON
- Net/HTTP (for handling requests and responses)
- Go (Download and install Go)
git clone https://github.com/your-username/movie-api-go.git
cd movie-api-gogo mod tidygo run main.goThe server will start at: http://localhost:8080
Endpoint: GET /movies
curl -X GET http://localhost:8080/moviesResponse:
[
{
"id": "1",
"isbn": "10",
"title": "MOVIE ONE",
"director": {
"firstname": "best",
"lastname": "director"
}
}
]Endpoint: GET /movie/{id}
curl -X GET http://localhost:8080/movie/1Response:
{
"id": "1",
"isbn": "10",
"title": "MOVIE ONE",
"director": {
"firstname": "best",
"lastname": "director"
}
}Endpoint: POST /movies
curl -X POST http://localhost:8080/movies \
-H "Content-Type: application/json" \
-d '{"isbn":"20","title":"NEW MOVIE","director":{"firstname":"John","lastname":"Doe"}}'Response:
{
"id": "87432912",
"isbn": "20",
"title": "NEW MOVIE",
"director": {
"firstname": "John",
"lastname": "Doe"
}
}Endpoint: PUT /movie/{id}
curl -X PUT http://localhost:8080/movie/1 \
-H "Content-Type: application/json" \
-d '{"isbn":"30","title":"UPDATED MOVIE","director":{"firstname":"Jane","lastname":"Smith"}}'Response:
{
"id": "1",
"isbn": "30",
"title": "UPDATED MOVIE",
"director": {
"firstname": "Jane",
"lastname": "Smith"
}
}Endpoint: DELETE /movie/{id}
curl -X DELETE http://localhost:8080/movie/1Response:
[
{
"id": "2",
"isbn": "11",
"title": "MOVIE TWO",
"director": {
"firstname": "best",
"lastname": "director"
}
}
]movie-api-go/
│── main.go # Main server logic
│── go.mod # Go module file
│── go.sum # Dependencies
type Movie struct {
ID string `json:"id"`
ISBN string `json:"isbn"`
Title string `json:"title"`
Director *Director `json:"director"`
}Why use JSON tags?
- When encoding/decoding JSON, the field names will match the specified JSON keys instead of struct field names.
params := mux.Vars(r) // Gets URL parameters as a mapExample: If request is GET /movie/1, then params["id"] == "1"
movies = append(movies[:index], movies[index+1:]...)- This removes the movie at
indexfrom themoviesslice. ...(spread operator) is used to unpack the remaining slice elements.
- Fork the repository
- Create a new feature branch (
git checkout -b feature-branch) - Commit your changes (
git commit -m "Added new feature") - Push the branch (
git push origin feature-branch) - Open a Pull Request
🚀 Happy Coding!