Skip to content

Commit 97b9f6b

Browse files
gin get endpoint by id
1 parent 8004ccb commit 97b9f6b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

gin-web-app/main.go

+17
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ func addUser(c *gin.Context) {
5757

5858
}
5959

60+
// / getAlbumByID locates the album whose ID value matches the id
61+
// parameter sent by the client, then returns that album as a response.
62+
func getUserByID(c *gin.Context) {
63+
id := c.Param("id")
64+
65+
// Loop over the list of albums, looking for
66+
// an album whose ID value matches the parameter.
67+
for _, a := range users {
68+
if a.ID == id {
69+
c.JSON(http.StatusOK, a)
70+
return
71+
}
72+
}
73+
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "user not found"})
74+
}
75+
6076
func main() {
6177
router := gin.Default()
6278

@@ -74,6 +90,7 @@ func main() {
7490
router.GET("/user/:name", getUser)
7591
router.GET("/user", getUsers)
7692
router.POST("/user", addUser)
93+
router.GET("/userData/:id", getUserByID)
7794

7895
router.Run(":3000")
7996
}

0 commit comments

Comments
 (0)