Created
November 3, 2019 04:31
-
-
Save ilhamarrouf/859309ac3a7f43956a428631b6389b2f to your computer and use it in GitHub Desktop.
GIN-Gionic binding uri
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "github.com/gin-gonic/gin" | |
| "net/http" | |
| ) | |
| type Person struct { | |
| ID string `uri:"id" binding:"required,uuid"` | |
| Name string `uri:"name" binding:"required"` | |
| } | |
| func main() { | |
| route := gin.Default() | |
| route.GET("/:name/:id", func(context *gin.Context) { | |
| var person Person | |
| if err := context.ShouldBindUri(&person); err != nil { | |
| context.JSON(http.StatusBadRequest, gin.H{ | |
| "message": err, | |
| }) | |
| } | |
| context.JSON(http.StatusOK, gin.H{ | |
| "uuid": person.ID, | |
| "name": person.Name, | |
| }) | |
| }) | |
| route.Run(":8080") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment