Skip to content

Commit 2ac5541

Browse files
committed
README.md updates
1 parent 391ea72 commit 2ac5541

File tree

2 files changed

+148
-1
lines changed

2 files changed

+148
-1
lines changed

README.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,126 @@
1-
# golang-crud-rest-api-gin
1+
# Golang CRUD REST API with Gin
2+
3+
This is a simple CRUD (Create, Read, Update, Delete) REST API in Go using the Gin web framework.
4+
5+
## Getting Started
6+
7+
Follow these instructions to get a copy of the project up and running on your local machine.
8+
9+
### Prerequisites
10+
11+
- Go (at least version 1.11)
12+
13+
### Cloning the Project
14+
15+
```bash
16+
git clone https://github.com/Shikha-code36/golang-crud-rest-api-gin.git
17+
cd golang-crud-rest-api-gin
18+
```
19+
20+
### Initializing the Project
21+
22+
```bash
23+
go mod init github.com/Shikha-code36/golang-crud-rest-api-gin
24+
```
25+
26+
### Downloading Dependencies
27+
28+
```bash
29+
go mod tidy
30+
```
31+
32+
## Running the Application
33+
34+
```bash
35+
go run main.go
36+
```
37+
38+
The application will start running at `http://localhost:8080`.
39+
40+
## Explanation of Code
41+
42+
### main.go
43+
44+
The `main.go` file is the entry point of our application. Here's a breakdown of its structure and functionality:
45+
46+
```go
47+
package main
48+
```
49+
This line defines the package that this file belongs to. In Go, the `main` package is special. It's the entry point of the application, and it must contain a `main` function.
50+
51+
```go
52+
import (
53+
"github.com/gin-gonic/gin"
54+
"github.com/Shikha-code36/golang-crud-rest-api-gin/handlers"
55+
)
56+
```
57+
This block imports two packages. The first one, `github.com/gin-gonic/gin`, is the Gin web framework that we're using to build our API. The second one, `github.com/Shikha-code36/golang-crud-rest-api-gin/handlers`, is the package that contains our handler functions.
58+
59+
```go
60+
func main() {
61+
r := gin.Default()
62+
```
63+
This is the `main` function where our application starts. `gin.Default()` creates a new Gin engine with some default middleware (logger and recovery). This engine is used to set up our routes and start the server.
64+
65+
```go
66+
r.GET("/users", handlers.GetUsers)
67+
r.GET("/user/:id", handlers.GetUser)
68+
r.POST("/create_user", handlers.CreateUser)
69+
r.PUT("/update_user/:id", handlers.UpdateUser)
70+
r.DELETE("/delete_user/:id", handlers.DeleteUser)
71+
```
72+
These lines define our API routes. For each route, we specify an HTTP method (GET, POST, PUT, DELETE), a path, and a handler function. When a request is received that matches a route's method and path, the corresponding handler function is called.
73+
74+
```go
75+
r.Run()
76+
}
77+
```
78+
Finally, `r.Run()` starts the Gin server and begins listening for requests. By default, it listens on `localhost:8080`, but you can pass a different address as an argument to `Run` if you want.
79+
80+
That's the basic structure of our `main.go` file. It sets up our server, defines our routes, and starts the server.
81+
82+
### users.go in handlers
83+
84+
The `users.go` file in the `handlers` directory contains the handler functions for our routes. You can find the detailed explanation [here](handlers).
85+
86+
## API Endpoints
87+
88+
- **GET /users**: Fetch all users.
89+
- **GET /user/:id**: Fetch a user by ID.
90+
- **POST /create_user**: Create a new user. The request body should be a JSON object with a `name` field, like this: `{"name": "Alice"}`. The ID will be assigned automatically.
91+
- **PUT /update_user/:id**: Update a user by ID. The request body should be a JSON object with a `name` field, like this: `{"name": "Bob"}`.
92+
- **DELETE /delete_user/:id**: Delete a user by ID.
93+
94+
## Testing the API
95+
96+
You can use a tool like curl or Postman to test the API. Here are some examples:
97+
98+
- Fetch all users:
99+
100+
```bash
101+
curl -X GET http://localhost:8080/users
102+
```
103+
104+
- Fetch a user by ID:
105+
106+
```bash
107+
curl -X GET http://localhost:8080/user/1
108+
```
109+
110+
- Create a new user:
111+
112+
```bash
113+
curl -X POST -H "Content-Type: application/json" -d '{"name":"Alice"}' http://localhost:8080/create_user
114+
```
115+
116+
- Update a user by ID:
117+
118+
```bash
119+
curl -X PUT -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/update_user/1
120+
```
121+
122+
- Delete a user by ID:
123+
124+
```bash
125+
curl -X DELETE http://localhost:8080/delete_user/1
126+
```

handlers/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#### handlers/users.go
2+
3+
- [users.go](users.go)
4+
5+
6+
**Imports:**
7+
8+
In Go, the `import` keyword is used to import other packages into the current file. The packages you import provide code that you can use in your current file. Here's what each import in your file does:
9+
10+
- `"github.com/gin-gonic/gin"`: Gin is a web framework written in Go. It features a martini-like API with performance that is up to 40 times faster. You're using it to set up your server and handle HTTP requests and responses.
11+
- `"net/http"`: This is a built-in Go package that provides HTTP client and server implementations. You're using it for the HTTP status codes (like `http.StatusOK`).
12+
- `"strconv"`: This is a built-in Go package that provides functions to convert strings to and from other data types. You're using the `strconv.Atoi` function to convert a string to an integer.
13+
14+
**`c *gin.Context`:**
15+
16+
In each of your handler functions, you have a parameter `c *gin.Context`. This is a context object provided by Gin. It's a struct that includes several useful fields and methods that you can use to handle HTTP requests and responses.
17+
18+
Here are a few things you can do with it:
19+
20+
- `c.Param("id")`: This gets the value of a URL parameter. In your case, you're using it to get the user ID from the URL.
21+
- `c.ShouldBindJSON(&user)`: This binds the JSON request body to a struct. You're using it to get the user data from the request body when creating or updating a user.
22+
- `c.JSON(http.StatusOK, user)`: This sends a JSON response with a status code. You're using it to send responses to the client.

0 commit comments

Comments
 (0)