Skip to content

Instantly share code, notes, and snippets.

@ilhamarrouf
Created November 3, 2019 04:31
Show Gist options
  • Select an option

  • Save ilhamarrouf/859309ac3a7f43956a428631b6389b2f to your computer and use it in GitHub Desktop.

Select an option

Save ilhamarrouf/859309ac3a7f43956a428631b6389b2f to your computer and use it in GitHub Desktop.
GIN-Gionic binding uri
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