Compare commits

...

5 Commits

Author SHA1 Message Date
pzx521521
7e2a397fa7
Merge 2ae5988db30756a7cf256d35019dc5151806387f into 49e9137c68e6dfaa529a2d0c9fe64d9e69a8554e 2025-04-17 17:20:45 +07:00
NezhaFan
49e9137c68
docs: fix comment (#4205)
Co-authored-by: voyager1 <voyager1@voyager1deMacBook-Pro.local>
2025-04-12 00:00:59 +08:00
Adlai Bridson-Boyczuk
1b53a47790
docs: Fixing English grammar mistakes and awkward sentence structure in doc/doc.md (#4207)
* docs: Fixing grammar mistakes and awkward sentences, such as modeling
binding section

* Update doc.md

Missed grammar mistake
2025-04-11 23:59:03 +08:00
Andrey Bolonin
3afff295a2
docs: add Upd language list (#4211)
* Upd language list

* Update url
2025-04-11 23:58:02 +08:00
pzx521521
2ae5988db3
Update xml.go
here is xml.Unmarshal problem if xml have muti-root:

[unmarshal-xml-array-in-golang-only-getting-the-first-element](https://stackoverflow.com/questions/27553274/unmarshal-xml-array-in-golang-only-getting-the-first-element)

[so  gin have same problem](https://stackoverflow.com/questions/74226570/golang-gin-binding-request-body-xml-to-slice):

if gin bind muti-root-xml, it should use for util io.EOF
2022-10-28 14:28:08 +08:00
5 changed files with 21 additions and 15 deletions

View File

@ -103,6 +103,7 @@ The documentation is also available on [gin-gonic.com](https://gin-gonic.com) in
- [Turkish](https://gin-gonic.com/tr/docs/) - [Turkish](https://gin-gonic.com/tr/docs/)
- [Persian](https://gin-gonic.com/fa/docs/) - [Persian](https://gin-gonic.com/fa/docs/)
- [Português](https://gin-gonic.com/pt/docs/) - [Português](https://gin-gonic.com/pt/docs/)
- [Russian](https://gin-gonic.com/ru/docs/)
### Articles ### Articles

View File

@ -34,7 +34,7 @@ func (protobufBinding) BindBody(body []byte, obj any) error {
if err := proto.Unmarshal(body, msg); err != nil { if err := proto.Unmarshal(body, msg); err != nil {
return err return err
} }
// Here it's same to return validate(obj), but util now we can't add // Here it's same to return validate(obj), but until now we can't add
// `binding:""` to the struct which automatically generate by gen-proto // `binding:""` to the struct which automatically generate by gen-proto
return nil return nil
// return validate(obj) // return validate(obj)

View File

@ -26,8 +26,13 @@ func (xmlBinding) BindBody(body []byte, obj any) error {
} }
func decodeXML(r io.Reader, obj any) error { func decodeXML(r io.Reader, obj any) error {
decoder := xml.NewDecoder(r) decoder := xml.NewDecoder(r)
if err := decoder.Decode(obj); err != nil { for {
return err if err := decoder.Decode(obj); err != nil {
if err == io.EOF{
break
}
return err
}
} }
return validate(obj) return validate(obj)
} }

View File

@ -70,7 +70,7 @@
### Build with json replacement ### Build with json replacement
Gin uses `encoding/json` as default json package but you can change it by build from other tags. Gin uses `encoding/json` as the default JSON package but you can change it by building from other tags.
[jsoniter](https://github.com/json-iterator/go) [jsoniter](https://github.com/json-iterator/go)
@ -84,7 +84,7 @@ go build -tags=jsoniter .
go build -tags=go_json . go build -tags=go_json .
``` ```
[sonic](https://github.com/bytedance/sonic) (you have to ensure that your cpu support avx instruction.) [sonic](https://github.com/bytedance/sonic) (you have to ensure that your cpu supports avx instruction.)
```sh ```sh
$ go build -tags="sonic avx" . $ go build -tags="sonic avx" .
@ -120,7 +120,7 @@ func main() {
router.HEAD("/someHead", head) router.HEAD("/someHead", head)
router.OPTIONS("/someOptions", options) router.OPTIONS("/someOptions", options)
// By default it serves on :8080 unless a // By default, it serves on :8080 unless a
// PORT environment variable was defined. // PORT environment variable was defined.
router.Run() router.Run()
// router.Run(":3000") for a hard coded port // router.Run(":3000") for a hard coded port
@ -172,7 +172,7 @@ func main() {
router := gin.Default() router := gin.Default()
// Query string parameters are parsed using the existing underlying request object. // Query string parameters are parsed using the existing underlying request object.
// The request responds to an url matching: /welcome?firstname=Jane&lastname=Doe // The request responds to a URL matching: /welcome?firstname=Jane&lastname=Doe
router.GET("/welcome", func(c *gin.Context) { router.GET("/welcome", func(c *gin.Context) {
firstname := c.DefaultQuery("firstname", "Guest") firstname := c.DefaultQuery("firstname", "Guest")
lastname := c.Query("lastname") // shortcut for c.Request.URL.Query().Get("lastname") lastname := c.Query("lastname") // shortcut for c.Request.URL.Query().Get("lastname")
@ -300,7 +300,7 @@ curl -X POST http://localhost:8080/upload \
#### Multiple files #### Multiple files
See the detail [example code](https://github.com/gin-gonic/examples/tree/master/upload-file/multiple). See the detailed [example code](https://github.com/gin-gonic/examples/tree/master/upload-file/multiple).
```go ```go
func main() { func main() {
@ -704,7 +704,7 @@ $ curl -v -X POST \
{"error":"Key: 'Login.Password' Error:Field validation for 'Password' failed on the 'required' tag"} {"error":"Key: 'Login.Password' Error:Field validation for 'Password' failed on the 'required' tag"}
``` ```
Skip validate: when running the above example using the above the `curl` command, it returns error. Because the example use `binding:"required"` for `Password`. If use `binding:"-"` for `Password`, then it will not return error when running the above example again. Skip-validation: Running the example above using the `curl` command returns an error. This is because the example uses `binding:"required"` for `Password`. If instead, you use `binding:"-"` for `Password`, then it will not return an error when you run the example again.
### Custom Validators ### Custom Validators
@ -1187,7 +1187,7 @@ func main() {
}) })
r.GET("/moreJSON", func(c *gin.Context) { r.GET("/moreJSON", func(c *gin.Context) {
// You also can use a struct // You can also use a struct
var msg struct { var msg struct {
Name string `json:"user"` Name string `json:"user"`
Message string Message string
@ -1490,7 +1490,7 @@ You may use custom delims
#### Custom Template Funcs #### Custom Template Funcs
See the detail [example code](https://github.com/gin-gonic/examples/tree/master/template). See the detailed [example code](https://github.com/gin-gonic/examples/tree/master/template).
main.go main.go
@ -1542,7 +1542,7 @@ Date: 2017/07/01
### Multitemplate ### Multitemplate
Gin allow by default use only one html.Template. Check [a multitemplate render](https://github.com/gin-contrib/multitemplate) for using features like go 1.6 `block template`. Gin allows only one html.Template by default. Check [a multitemplate render](https://github.com/gin-contrib/multitemplate) for using features like go 1.6 `block template`.
### Redirects ### Redirects
@ -2091,7 +2091,7 @@ type formB struct {
func SomeHandler(c *gin.Context) { func SomeHandler(c *gin.Context) {
objA := formA{} objA := formA{}
objB := formB{} objB := formB{}
// This c.ShouldBind consumes c.Request.Body and it cannot be reused. // Calling c.ShouldBind consumes c.Request.Body and it cannot be reused.
if errA := c.ShouldBind(&objA); errA == nil { if errA := c.ShouldBind(&objA); errA == nil {
c.String(http.StatusOK, `the body should be formA`) c.String(http.StatusOK, `the body should be formA`)
// Always an error is occurred by this because c.Request.Body is EOF now. // Always an error is occurred by this because c.Request.Body is EOF now.
@ -2324,7 +2324,7 @@ or network CIDRs from where clients which their request headers related to clien
IP can be trusted. They can be IPv4 addresses, IPv4 CIDRs, IPv6 addresses or IP can be trusted. They can be IPv4 addresses, IPv4 CIDRs, IPv6 addresses or
IPv6 CIDRs. IPv6 CIDRs.
**Attention:** Gin trust all proxies by default if you don't specify a trusted **Attention:** Gin trusts all proxies by default if you don't specify a trusted
proxy using the function above, **this is NOT safe**. At the same time, if you don't proxy using the function above, **this is NOT safe**. At the same time, if you don't
use any proxy, you can disable this feature by using `Engine.SetTrustedProxies(nil)`, use any proxy, you can disable this feature by using `Engine.SetTrustedProxies(nil)`,
then `Context.ClientIP()` will return the remote address directly to avoid some then `Context.ClientIP()` will return the remote address directly to avoid some

View File

@ -154,7 +154,7 @@ func RunUnix(file string) (err error) {
// RunFd attaches the router to a http.Server and starts listening and serving HTTP requests // RunFd attaches the router to a http.Server and starts listening and serving HTTP requests
// through the specified file descriptor. // through the specified file descriptor.
// Note: the method will block the calling goroutine indefinitely unless on error happens. // Note: the method will block the calling goroutine indefinitely unless an error happens.
func RunFd(fd int) (err error) { func RunFd(fd int) (err error) {
return engine().RunFd(fd) return engine().RunFd(fd)
} }