``` go
func main() {
r := gin.Default()
// r.GET("/JSONP?callback=x", func(c *gin.Context) { // old
r.GET("/JSONP", func(c *gin.Context) { // new
data := gin.H{
"foo": "bar",
}
//callback is x
// Will output : x({\"foo\":\"bar\"})
c.JSONP(http.StatusOK, data)
})
// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}
// client
// curl http://127.0.0.1:8080/JSONP?callback=x
// old output
// 404 page not found
// new output
// x({"foo":"bar"})
```
Most of the sample code in the documentation map[string]interface{} is represented by gin.H.
gin.H is a very important place for me to like gin, can write a lot less code
* support bind http header param #1956
update #1956
```
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
type testHeader struct {
Rate int `header:"Rate"`
Domain string `header:"Domain"`
}
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
h := testHeader{}
if err := c.ShouldBindHeader(&h); err != nil {
c.JSON(200, err)
}
fmt.Printf("%#v\n", h)
c.JSON(200, gin.H{"Rate": h.Rate, "Domain": h.Domain})
})
r.Run()
// client
// curl -H "rate:300" -H "domain:music" 127.0.0.1:8080/
// output
// {"Domain":"music","Rate":300}
}
```
* add unit test
* Modify the code to get the http header
When the http header is obtained in the standard library,
the key value will be modified by the CanonicalMIMEHeaderKey function,
and finally the value of the http header will be obtained from the map.
As follows.
```go
func (h MIMEHeader) Get(key string) string {
// ...
v := h[CanonicalMIMEHeaderKey(key)]
// ...
}
```
This pr also follows this modification
* Thanks to vkd for suggestions, modifying code
* Increase test coverage
env GOPATH=`pwd` go test github.com/gin-gonic/gin/binding -coverprofile=cover.prof
ok github.com/gin-gonic/gin/binding 0.015s coverage: 100.0% of statements
* Rollback check code
* add use case to README.md
* refactor(form_mapping.go): mapping ptr, struct and map
* fix#1672 correct work with ptr - not create value if field is not set
* avoid allocations on strings.Split() - change to strings.Index()
* fix#610 tag value "-" is mean ignoring field
* struct fields mapped like json.Unmarshal
* map fields mapped like json.Unmarshal
* fix after @thinkerou review
Add a function `ForceConsoleColor`, like `DisableConsoleColor` but to force coloring the output.
It usefull when some IDE's integrated console (like IntelliJ or Goland) are not detected as TTY, but can display colors.
Also helps if one want to output color in log file (#1590) and as a workaround for #1547.
* support bind uri (1)
* uri binding successful run
* fix vet warning: github.com/gin-gonic/gin/internal.Param composite literal uses unkeyed fields
* fix code style
* update function name
* fix test function signature
* add test for CanSet
* update readme and add test case
* remove internal.Params
* add coverage
* fix warning
`Gin` now have the `protobufBinding` function to check the request format, but didn't have a protobuf response function like `c.YAML()`.
In our company [ByteDance](http://bytedance.com/), the largest internet company using golang in China, we use `gin` to transfer __Protobuf__ instead of __Json__, we have to write some internal library to make some wrappers to achieve that, and the code is not elegant. So we really want such a feature.