This is a simple CRUD example with using Gin.
Get
Purpose
Hard code a list of user data.
Create a GET endpoint to fetch all data.
Extract service layer to handle the requests.
Code
main.go
package mainimport ( "first-gin/service" "github.com/gin-gonic/gin" ) func main () { router := gin.Default() router.GET("/users" , service.FetchAll) router.Run(":8080" ) }
service/user.go
package serviceimport ( "first-gin/models" "github.com/gin-gonic/gin" ) var useList = []models.User{ {ID: 1 , Name: "John" , Email: "john@mail" }, {ID: 2 , Name: "Doe" , Email: "doe@mail" }, } func FetchAll (c *gin.Context) { c.JSON(200 , gin.H{ "message" : useList, }) }
models/user.go
package modelstype User struct { ID int `json:"id"` Name string `json:"name"` Email string `json:"email"` }
Explanation
Create a routing for /users
endpoint.
Create a service layer to handle the requests and extract the logic from the main.go.
Create a variable to store the user data.
Create a function to fetch all data.
Return the data to the client.
Create a models for the user data.
Use json tag to map the data to the struct.
E2E Test
curl http://localhost:8080/users
we should see the response:
{ "message" : [ { "id" : 1 , "name" : "John" , "email" : "john@mail" } , { "id" : 2 , "name" : "Doe" , "email" : "doe@mail" } ] }
Post
Purpose
Create a POST endpoint to insert data.
Add function in the service layer to handle the request.
Parse the data from request body into model type.
Add the data to the list.
Return the list.
Code
main.go
package mainimport ("first-gin/service" "github.com/gin-gonic/gin" ) func main () { router := gin.Default() router.POST("" , service.Insert) router.Run(":8080" ) }
service/user.go
func Insert (c *gin.Context) { var user models.User c.BindJSON(&user) useList = append (useList, user) c.JSON(200 , gin.H{ "message" : useList, }) }
Explanation
first, we added a router for the POST request.
In the service layer, we added a function to handle the POST request.
Declared a null user model.
Use BindJSON
to bind the request body to the user model.
Append the parsed data to the list.
Return the list as a response.
E2E Testing
we cat use curl to test the POST request.
curl -X POST http://localhost:8080 -d '{"ID": 3, "Name": "Jane", "Email": "jane@mail"}'
we should see the response:
{ "message" : [ { "id" : 1 , "name" : "John" , "email" : "john@mail" } , { "id" : 2 , "name" : "Doe" , "email" : "doe@mail" } , { "id" : 3 , "name" : "Jane" , "email" : "jane@mail" } ] }
Delete
Purpose
New endpoint to delete data.
Add a function in the service layer to handle the request.
Extract the ID from the request parameter(Restful).
Remove the data from the list.
Return the list.
Code
main.go
package mainimport ( "first-gin/service" "github.com/gin-gonic/gin" ) func main () { router := gin.Default() router.GET("/users" , service.FetchAll) router.POST("" , service.Insert) router.DELETE("user/:id" , service.Delete) router.Run(":8080" ) }
service/user.go
func Delete (context *gin.Context) { id, _ := strconv.Atoi(context.Param("id" )) useList = Filter(useList, func (user models.User) bool { return user.ID != id }) context.JSON(200 , gin.H{ "message" : useList, }) } func Filter (list []models.User, predicate func (user models.User) bool ) []models.User { var result []models.User for _, user := range list { if predicate(user) { result = append (result, user) } } return result }
Explanation
Added a new route for the DELETE request in main.go.
In the service layer, added a function to handle the DELETE request.
Extract the ID from the request parameter.
New a function Filter
and make it receive a func as a parameter
For the predicate function, we check if the user ID is not equal to the ID we want to delete.
Return the list as a response.
return the list as a response.