Skip to content
Myr

修改连接错误,修正一些报引用错误 (#1655)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

修复了全角括号导致超链接不能正常访问的错误。 修复了一些URL中的"/"被改成" / "的错误。 修复了一些包引用中"/"被改成" / "的错误。 修复有超链接被翻译成中文的错误。

authored by 尹宝强 · committed by thinkerou · parent 6053156 · Browse files · 1 file+34 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+34 added−34 removed

  • other:+34 added−34 removed

1 file+34 added−34 removed

modified:README_ZH.md+34 added-34 removed
77 unmodified lines
78```go79import "github.com/gin-gonic/gin"80```81823. (可选)如果使用诸如`http.StatusOK`之类的常量,则需要引入 `net / http` 包。823. (可选)如果使用诸如`http.StatusOK`之类的常量,则需要引入 `net/http` 包。8384```go85import "net/http"86```
112 unmodified lines
199- [x] API冻结,新版本不会破坏您的代码。200201## 使用 [jsoniter](https://github.com/json-iterator/go) 构建202203Gin使用`encoding / json`作为默认的json包,但您可以通过其他标签的构建更改为[jsoniter](https://github.com/json-iterator/go)。203Gin使用`encoding/json`作为默认的json包,但您可以通过其他标签的构建更改为[jsoniter](https://github.com/json-iterator/go)。204205```sh206$ go build -tags=jsoniter .207```
150 unmodified lines
358### 上传文件359360#### 单个文件上传361362参考问题[#774](https://github.com/gin-gonic/gin/issues/774)和详细[示例代码](examples / upload-file / single)。362参考问题[#774](https://github.com/gin-gonic/gin/issues/774)和详细[示例代码](examples/upload-file/single)。363364```go365func main() {366 router := gin.Default()
22 unmodified lines
389```390391#### 多文件上传392393查看详细信息[示例代码](examples / upload-file / multiple)。393查看详细信息[示例代码](examples/upload-file/multiple)。394395```go396func main() {397 router := gin.Default()
131 unmodified lines
529### 模型绑定和验证530531要将请求主体绑定到类型中,请使用模型绑定。我们目前支持JSON,XML和标准表单值的绑定(foo = bar&boo = baz)。532533Gin使用[** go-playground / validator.v8 **](https://github.com/go-playground/validator)进行验证。检查有关标签用法的完整文档[此处](http://godoc.org/gopkg.in/go-playground/validator.v8#hdr-Baked_In_Validators_and_Tags)。533Gin使用[** go-playground/validator.v8 **](https://github.com/go-playground/validator)进行验证。检查有关标签用法的完整文档[此处](http://godoc.org/gopkg.in/go-playground/validator.v8#hdr-Baked_In_Validators_and_Tags)。534535请注意,您需要在要绑定的所有字段上设置相应的绑定标记。例如,从JSON绑定时,设置`json:“fieldname”`。536537此外,Gin提供了两组绑定方法:538- **类型** - 必须绑定539   - **方法** - `Bind`,`BindJSON`,`BindXML`,`BindQuery`540   - **行为** - 这些方法在引擎盖下使用`MustBindWith`。如果存在绑定错误,则使用`c.AbortWithError(400,err).SetType(ErrorTypeBind)`中止请求。这将响应状态代码设置为400,并将`Content-Type`标头设置为`text / plain;字符集= UTF-8`。请注意,如果您在此之后尝试设置响应代码,则会发出警告“[GIN-debug] [警告]标题已经写入。想用422`覆盖状态代码400。如果您希望更好地控制行为,请考虑使用`ShouldBind`等效方法。540   - **行为** - 这些方法在引擎盖下使用`MustBindWith`。如果存在绑定错误,则使用`c.AbortWithError(400,err).SetType(ErrorTypeBind)`中止请求。这将响应状态代码设置为400,并将`Content-Type`标头设置为`text/plain;字符集= UTF-8`。请注意,如果您在此之后尝试设置响应代码,则会发出警告“[GIN-debug] [警告]标题已经写入。想用422`覆盖状态代码400。如果您希望更好地控制行为,请考虑使用`ShouldBind`等效方法。541- **类型** - 应该绑定542   - **方法** - `ShouldBind`,`ShouldBindJSON`,`ShouldBindXML`,`ShouldBindQuery`543   - **行为** - 这些方法在引擎盖下使用`ShouldBindWith`。如果存在绑定错误,则返回错误,开发人员有责任正确处理请求和错误。544
97 unmodified lines
642使用上面 的`curl` 命令运行上面的例子时,它返回错误。 因为这个例子使用 `binding:'需要``````。 如果使用 `binding:“ - ``````,那么在再次运行上面的例子时它不会返回错误。643644### 自定义验证器645646也可以注册自定义验证器。 请参阅[示例代码](examples / custom-validation / server.go)。646也可以注册自定义验证器。 请参阅[示例代码](examples/custom-validation/server.go)。647648[embedmd]:#(examples / custom-validation / server.go go)648[embedmd]:#(examples/custom-validation/server.go go)649```go650package main651652import (
53 unmodified lines
706$ curl "localhost:8085/bookable?check_in=2018-03-08&check_out=2018-03-09"707{"error":"Key: 'Booking.CheckIn' Error:Field validation for 'CheckIn' failed on the 'bookabledate' tag"}708```709710[结构级验证](https://github.com/go-playground/validator/releases/tag/v8.7)也可以这种方式注册。711请参阅[struct-lvl-validation示例](examples / struct-lvl-validations)以了解更多信息。710[结构级验证](https://github.com/go-playground/validator/releases/tag/v8.7)也可以这种方式注册。711请参阅[struct-lvl-validation示例](examples/struct-lvl-validations)以了解更多信息。712713### 只绑定查询字符串714715`ShouldBindQuery` 函数只绑定查询参数而不是后期数据。 请参阅[详细信息](https://github.com/gin-gonic/gin/issues/742#issuecomment-315953017)。715`ShouldBindQuery` 函数只绑定查询参数而不是后期数据。 请参阅[详细信息](https://github.com/gin-gonic/gin/issues/742#issuecomment-315953017)。716717```go718package main719
27 unmodified lines
747```748749### 绑定查询字符串或发布数据750751请参阅[详细信息](https://github.com/gin-gonic/gin/issues/742#issuecomment-264681292)。751请参阅[详细信息](https://github.com/gin-gonic/gin/issues/742#issuecomment-264681292)。752753```go754package main755
37 unmodified lines
793```794795### 绑定 HTML 复选框796797参见[详细信息](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092)797参见[详细信息](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092)798799main.go800801```go
128 unmodified lines
930```931932#### SecureJSON933934使用 SecureJSON 来防止 json 劫持。 如果给定的结构是数组值,则默认预 置`“while(1),”` 到响应体。934使用 SecureJSON 来防止 json 劫持。 如果给定的结构是数组值,则默认预 置`“while(1),”` 到响应体。935936```go937func main() {938 r := gin.Default()
127 unmodified lines
1066```10671068### HTML 渲染10691070使用LoadHTMLGlob()或LoadHTMLFiles()1070使用LoadHTMLGlob()或LoadHTMLFiles()10711072```go1073func main() {1074 router := gin.Default()
83 unmodified lines
11581159```go1160 r := gin.Default()1161 r.Delims("{[{", "}]}")1162 r.LoadHTMLGlob("/path/to/templates"))1162 r.LoadHTMLGlob("/path/to/templates")1163```11641165#### 自定义模板功能11661167查看详细信息[示例代码](示例/模板)。1167查看详细信息[示例代码](examples/template)。11681169main.go11701171```go
42 unmodified lines
1214```12151216### 多模板12171218Gin 允许默认只使用一个 html 模板。 检查[多模板渲染](https://github.com/gin-contrib/multitemplate)以使用 go 1.6 `block template` 等功能。1218Gin 允许默认只使用一个 html 模板。 检查[多模板渲染](https://github.com/gin-contrib/multitemplate)以使用 go 1.6 `block template` 等功能。12191220### 重定向12211222Issuing a HTTP redirect is easy. Both internal and external locations are supported.
131 unmodified lines
1354```13551356### 自定义 HTTP 配置13571358直接使用`http.ListenAndServe()`,如下所示:1358直接使用`http.ListenAndServe()`,如下所示:13591360```go1361func main() {1362 router := gin.Default()
77 unmodified lines
1440```14411442### 使用 Gin 运行多个服务14431444请参阅[问题](https://github.com/gin-gonic/gin/issues/346)并尝试以下示例:1444请参阅[问题](https://github.com/gin-gonic/gin/issues/346)并尝试以下示例:14451446[embedmd]:# (examples/multiple-service/main.go go)1447```go1448package main
76 unmodified lines
15251526您想要优雅地重启或停止您的Web服务器吗?1527有一些方法可以做到这一点。15281529我们可以使用[fvbock / endless](https://github.com/fvbock/endless)来替换默认的`ListenAndServe`。 有关更多详细信息,请参阅问题[#296](https://github.com/gin-gonic/gin/issues/296)。1529我们可以使用[fvbock/endless](https://github.com/fvbock/endless)来替换默认的`ListenAndServe`。 有关更多详细信息,请参阅问题[#296](https://github.com/gin-gonic/gin/issues/296)。15301531```go1532router := gin.Default()1533router.GET("/", handler)
2 unmodified lines
1536```15371538另一种替代方案:15391540* [manners](https://github.com/braintree/manners):礼貌的Go HTTP服务器,可以正常关闭。1541* [graceful](https://github.com/tylerb/graceful):Graceful是一个Go包,可以正常关闭http.Handler服务器。1542* [grace](https://github.com/facebookgo/grace):Go服务器的平滑重启和零停机时间部署。1540* [manners](https://github.com/braintree/manners):礼貌的Go HTTP服务器,可以正常关闭。1541* [graceful](https://github.com/tylerb/graceful):Graceful是一个Go包,可以正常关闭http.Handler服务器。1542* [grace](https://github.com/facebookgo/grace):Go服务器的平滑重启和零停机时间部署。15431544如果您使用的是Go 1.8,则可能不需要使用此库! 考虑使用http.Server的内置[Shutdown()](https://golang.org/pkg/net/http/#Server.Shutdown)方法进行正常关机。 请参阅gin的完整[graceful-shutdown](./ examples / graceful-shutdown)示例。1544如果您使用的是Go 1.8,则可能不需要使用此库! 考虑使用http.Server的内置[Shutdown()](https://golang.org/pkg/net/http/#Server.Shutdown)方法进行正常关机。 请参阅gin的完整[graceful-shutdown](./examples /graceful-shutdown)示例。15451546[embedmd]:# (examples/graceful-shutdown/graceful-shutdown/server.go go)1547```go1548// +build go1.8
87 unmodified lines
1636 return t, nil1637}1638```16391640请参阅`examples / assets-in-binary`目录中的完整示例。1640请参阅`examples/assets-in-binary`目录中的完整示例。16411642### Bind form-data request with custom struct16431644以下示例使用自定义结构:
142 unmodified lines
1787对性能有轻微影响,所以如果你这样做,你不应该使用这种方法1788足以立刻调用绑定。1789*只有某些格式需要此功能 - “JSON”,“XML”,“MsgPack”,1790`ProtoBuf`。 对于其他格式,`Query`,`Form`,`FormPost`,`FormMultipart`,1791可以被`c.ShouldBind()`多次调用而不会造成任何损害1792表现(见[#1341](https://github.com/gin-gonic/gin/pull/1341))。1791可以被`c.ShouldBind()`多次调用而不会造成任何损害1792表现(见[#1341](https://github.com/gin-gonic/gin/pull/1341)。17931794### http2 server 推送179517961797http.Pusher仅支持** go1.8 + **。 有关详细信息,请参阅[golang blog](https://blog.golang.org/h2push)。1797http.Pusher仅支持** go1.8 + **。 有关详细信息,请参阅[golang blog](https://blog.golang.org/h2push)。17981799[embedmd]:# (examples/http-pusher/main.go go)1800```go1801package main
83 unmodified lines
188518861887## 测试18881889`net / http / httptest`包是HTTP测试的首选方式。1889`net/http/httptest`包是HTTP测试的首选方式。18901891```go1892package main1893
38 unmodified lines
19321933## 用户193419351936使用[Gin](https://github.com/gin-gonic/gin)Web框架的令人敬畏的项目列表。1936使用[Gin](https://github.com/gin-gonic/gin)Web框架的令人尊敬的项目列表。19371938* [drone](https://github.com/drone/drone):drone,用Go编写。1939* [gorush](https://github.com/appleboy/gorush):用Go编写的推送通知服务器。1940* [fnproject](https://github.com/fnproject/fn):容器本机,云无关的无服务器平台。1938* [drone](https://github.com/drone/drone):drone,用Go编写。1939* [gorush](https://github.com/appleboy/gorush):用Go编写的推送通知服务器。1940* [fnproject](https://github.com/fnproject/fn):容器本机,云无关的无服务器平台。