Skip to content
Myr

调整描述语句 (#1657)

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

调整描述语句

authored by weibaohui · committed by thinkerou · parent 1f576fb · Browse files · 1 file+46 added−49 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+46 added−49 removed

  • other:+46 added−49 removed

1 file+46 added−49 removed

modified:README_ZH.md+46 added-49 removed
23 unmodified lines
24- [Gin v1 稳定版](#gin-v1-稳定版)25- [使用 jsoniter 构建](#使用-jsoniter-构建)26- [API 示例](#api-示例)27 - [GET,POST,PUT,PATCH,DELETE,OPTIONS 使用](#get-post-put-patch-delete-options-使用)28 - [路由参数](#路由参数)29 - [查询字符串参数](#查询字符串参数)28 - [获取路由参数](#获取路由参数)29 - [获取url查询参数](#获取url查询参数)30 - [Multipart Urlencoded 表单](#multipart-urlencoded-表单)31 - [另一个实列 query + post form](#另一个实列:-query-+-post-form)31 - [获取post表单数据(url带查询参数)](#获取post表单数据(url带查询参数))32 - [映射参数 表单参数](#映射参数-表单参数)33 - [上传文件](#上传文件)34 - [路由组](#路由组)35 - [默认初始化 Gin](#默认初始化-gin)36 - [中间件使用](#中间件使用)36 - [使用中间件](#使用中间件)37 - [如何记录日志](#如何记录日志)38 - [模型绑定和验证](#模型绑定和验证)39 - [自定义验证器](#自定义验证器)40 - [只绑定查询字符串](#只绑定查询字符串)41 - [绑定查询字符串或发布数据](#绑定查询字符串或发布数据)40 - [只绑定url查询参数](#只绑定url查询参数)41 - [url查询参数绑定到struct(或POST表单数据)](#url查询参数绑定到struct(或POST表单数据))42 - [绑定 HTML 复选框](#绑定-html-复选框)43 - [Multipart Urlencoded 绑定](#multipart-urlencoded-绑定)44 - [XML JSON YAML ProtoBuf 渲染](#xml-json-yaml-protobuf-渲染)45 - [SecureJSON](#SecureJSON)
9 unmodified lines
55 - [Let's Encrypt 支持](#lets-encrypt-支持)56 - [使用 Gin 运行多个服务](使用-gin-运行多个服务)57 - [优雅重启或停止](#优雅重启或停止)58 - [使用模板构建单个二进制文件](#使用模板构建单个二进制文件)59 - [使用自定义结构绑定表单数据请求](#使用自定义结构绑定表单数据请求)60 - [尝试将body绑定到不同的结构中](#尝试将-body-绑定到不同的结构中)59 - [表单数据绑定到自定义结构体](#表单数据绑定到自定义结构体)60 - [将request body绑定到不同的结构体中](#将request body绑定到不同的结构体中)61 - [http2 server 推送](#http2-server-推送)62 - [定义路由日志的格式](#定义路由日志的格式)63- [测试](#测试)64- [用户](#用户)
39 unmodified lines
104$ govendor init105$ govendor fetch github.com/gin-gonic/gin@v1.3106```1071084. 复制一个启动文件模板到项目目录中1084. 复制启动文件模板到项目目录中109110```sh111$ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go112```
5 unmodified lines
118```119120## 前提条件121122新版本的 Gin 需要 Go 1.6 或者更高版本并且很快就会升级到 Go 1.7.122新版本的 Gin 需要 Go 1.6 或者更高版本,并且很快就会升级到 Go 1.7.123124## 快速启动125 126```sh
106 unmodified lines
233 // router.Run(":3000") for a hard coded port234}235```236237### 路由参数237### 获取路由参数238239```go240func main() {241 router := gin.Default()
16 unmodified lines
258 router.Run(":8080")259}260```261262### 查询字符串参数262### 获取url查询参数263264```go265func main() {266 router := gin.Default()
29 unmodified lines
296 router.Run(":8080")297}298```299300### 另一个实列 query + post form300### 获取post表单数据(url带查询参数)301302```303POST /post?id=1234&page=1 HTTP/1.1304Content-Type: application/x-www-form-urlencoded
74 unmodified lines
379 router.Run(":8080")380}381```382383如何 `curl`:383`curl`示例:384385```bash386curl -X POST http://localhost:8080/upload \387 -F "file=@/Users/appleboy/test.zip" \
25 unmodified lines
413 router.Run(":8080")414}415```416417如何 `curl`:417`curl`示例:418419```bash420curl -X POST http://localhost:8080/upload \421 -F "upload[]=@/Users/appleboy/test1.zip" \
42 unmodified lines
464r := gin.Default()465```466467468### 中间件使用468### 使用中间件469```go470func main() {471 // Creates a router without any middleware by default472 r := gin.New()
236 unmodified lines
709710[结构级验证](https://github.com/go-playground/validator/releases/tag/v8.7)也可以这种方式注册。711请参阅[struct-lvl-validation示例](examples/struct-lvl-validations)以了解更多信息。712713### 只绑定查询字符串713### 只绑定url查询参数714715`ShouldBindQuery` 函数只绑定查询参数而不是后期数据。 请参阅[详细信息](https://github.com/gin-gonic/gin/issues/742#issuecomment-315953017)。715`ShouldBindQuery` 函数只绑定url查询参数而不是post字段。 请参阅[详细信息](https://github.com/gin-gonic/gin/issues/742#issuecomment-315953017)。716717```go718package main719
25 unmodified lines
745}746747```748749### 绑定查询字符串或发布数据749### url查询参数绑定到struct(或POST表单数据)750751请参阅[详细信息](https://github.com/gin-gonic/gin/issues/742#issuecomment-264681292)。752753```go
117 unmodified lines
871 router.Run(":8080")872}873```874875Test it with:875测试:876```sh877$ curl -v --form user=user --form password=password http://localhost:8080/login878```879
118 unmodified lines
998```9991000#### PureJSON10011002通常,JSON 用其 unicod e实体替换特殊 HTML 字符,例如 `<` 变为 `\ u003c`。 如果要按字面意思对这些字符进行编码,则可以使用 PureJSON。1003Go 1.6 及更低版本无法使用此功能。1002通常,JSON使用unicode替换特殊HTML字符,例如 `<` 变为 `\ u003c`。 如果要按字面意思对这些字符进行编码,则可以使用 PureJSON。1003Go 1.6及更低版本无法使用此功能。10041005```go1006func main() {1007 r := gin.Default()
373 unmodified lines
1381```13821383### Let's Encrypt 支持13841385单行 LetsEncrypt HTTPS 服务器的示例。1385一行代码支持 LetsEncrypt HTTPS示例。13861387[embedmd]:# (examples/auto-tls/example1/main.go go)1388```go1389package main
16 unmodified lines
1406 log.Fatal(autotls.Run(r, "example1.com", "example2.com"))1407}1408```14091410自定义autocert管理器的示例。1410autocert使用示例。14111412[embedmd]:# (examples/auto-tls/example2/main.go go)1413```go1414package main
119 unmodified lines
1534// [...]1535endless.ListenAndServe(":4242", router)1536```15371538另一种替代方案:1538替代方案: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服务器的平滑重启和零停机时间部署。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
47 unmodified lines
1596```15971598### 使用模板构建单个二进制文件15991600您可以使用[go-assets] []将服务器构建到包含模板的单个二进制文件中。1600您可以使用[go-assets]将服务器打包为包含模板的单个二进制可执行文件。16011602[go-assets]:https://github.com/jessevdk/go-assets16031604```go
33 unmodified lines
1638```16391640请参阅`examples/assets-in-binary`目录中的完整示例。16411642### Bind form-data request with custom struct1642### 表单数据绑定到自定义结构体16431644以下示例使用自定义结构:16451646```go
83 unmodified lines
1730```17311732总之,只支持现在没有`form`的嵌套自定义结构。17331734### 尝试将 body 绑定到不同的结构中1734### 将request body绑定到不同的结构体中17351736绑定请求体的常规方法使用`c.Request.Body`和它们1737不能多次调用。1736一般通过调用`c.Request.Body`方法绑定数据,但不能多次调用这个方法。17371738```go1739type formA struct {1740 Foo string `json:"foo" xml:"foo" binding:"required"`
17 unmodified lines
1758 }1759}1760```17611763为此,您可以使用`c.ShouldBindBodyWith`.1762为此,要想多次绑定,需要使用`c.ShouldBindBodyWith`.17631764```go1765func SomeHandler(c *gin.Context) {1766 objA := formA{}
13 unmodified lines
1780}1781```178217831785482/50001786*`c.ShouldBindBodyWith`在绑定之前将body存储到上下文中。 这有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)。1784*`c.ShouldBindBodyWith`会在绑定之前将body存储到上下文中。 这会1785对性能造成轻微影响,如果调用一次就能完成绑定的话,那就不要用这个方法。1786*只有某些格式需要此功能 ,如“JSON”,“XML”,“MsgPack”,1787`ProtoBuf`。 对于其他格式,如`Query`,`Form`,`FormPost`,`FormMultipart`,1788可以多次调用`c.ShouldBind()`而不会造成任任何性能损失(见[#1341](https://github.com/gin-gonic/gin/pull/1341))。17891790### http2 server 推送17911792
45 unmodified lines
1838```18391840### 定义路由日志的格式18411846路由的默认日志是:1842默认的路由日志格式:1843```1844[GIN-debug] POST /foo --> main.main.func1 (3 handlers)1845[GIN-debug] GET /bar --> main.main.func2 (3 handlers)1846[GIN-debug] GET /status --> main.main.func3 (3 handlers)1847```18481853如果要以给定格式记录此信息(例如JSON,键值或其他内容),则可以使用`gin.DebugPrintRouteFunc`定义此格式。1854在下面的示例中,我们使用标准日志包记录所有路由,但您可以使用其他适合您需求的日志工具。1849如果要以指的格式(例如JSON,Key Values或其他格式)记录信息,则可以使用`gin.DebugPrintRouteFunc`指定格式。1850在下面的示例中,我们使用标准日志包记录所有路由,但您可以使用其他满足需求的日志工具。1851```go1852import (1853 "log"1854 "net/http"
26 unmodified lines
188118821883## 测试18841889`net/http/httptest`包是HTTP测试的首选方式。1885HTTP测试首选`net/http/httptest`包。18861887```go1888package main1889
10 unmodified lines
1900 r.Run(":8080")1901}1902```19031908测试上面的代码示例:1904上面这段代码的测试用例:19051906```go1907package main1908
19 unmodified lines
19281929## 用户193019311936使用[Gin](https://github.com/gin-gonic/gin)Web框架的令人尊敬的项目列表。1932使用[Gin](https://github.com/gin-gonic/gin)框架的著名项目。19331938* [drone](https://github.com/drone/drone):drone,用Go编写。1939* [gorush](https://github.com/appleboy/gorush):用Go编写的推送通知服务器。1940* [fnproject](https://github.com/fnproject/fn):容器本机,云无关的无服务器平台。1934* [drone](https://github.com/drone/drone):用Go编写的基于docker的持续集成平台。1935* [gorush](https://github.com/appleboy/gorush):用Go编写的推送通知服务。1936* [fnproject](https://github.com/fnproject/fn):容器驱动、云无关的无服务器平台。1937* [photoprism](https://github.com/photoprism/photoprism): 用Go编写的基于TensorFlow的个人相册管理系统.