From 5557aac3a812b6222c47582b8f91c856db5dd7df Mon Sep 17 00:00:00 2001 From: Anuchit Prasertsang <3372362+AnuchitO@users.noreply.github.com> Date: Tue, 10 Sep 2024 16:10:53 +0700 Subject: [PATCH] update example ShouldBind flip logic to make it more easy to understand and follow Go's idiomatic way. so that people who new to Go will have less confuse. --- content/en/docs/examples/bind-query-or-post.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/content/en/docs/examples/bind-query-or-post.md b/content/en/docs/examples/bind-query-or-post.md index 7ebbe739c..2a47e827e 100644 --- a/content/en/docs/examples/bind-query-or-post.md +++ b/content/en/docs/examples/bind-query-or-post.md @@ -32,12 +32,16 @@ func startPage(c *gin.Context) { // If `GET`, only `Form` binding engine (`query`) used. // If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`). // See more at https://github.com/gin-gonic/gin/blob/master/binding/binding.go#L48 - if c.ShouldBind(&person) == nil { - log.Println(person.Name) - log.Println(person.Address) - log.Println(person.Birthday) + err := c.ShouldBind(&person) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return } + log.Println(person.Name) + log.Println(person.Address) + log.Println(person.Birthday) + c.String(200, "Success") } ```