Skip to content
Myr

refactor(doc): use space not tab (#1006)

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

authored by 田欧 · committed by Bo-Yi Wu · parent 02a6f9b · Browse files · 1 file+32 added−31 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+32 added−31 removed

  • other:+32 added−31 removed

1 file+32 added−31 removed

modified:context.go+32 added-31 removed
83 unmodified lines
84 cp.handlers = nil85 return &cp86}8788// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()", this89// function will return "main.handleGetUsers"88// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()",89// this function will return "main.handleGetUsers"90func (c *Context) HandlerName() string {91 return nameOfFunction(c.handlers.Last())92}93
22 unmodified lines
116 return c.index >= abortIndex117}118119// Abort prevents pending handlers from being called. Note that this will not stop the current handler.120// Let's say you have an authorization middleware that validates that the current request is authorized. If the121// authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers120// Let's say you have an authorization middleware that validates that the current request is authorized.121// If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers122// for this request are not called.123func (c *Context) Abort() {124 c.index = abortIndex125}
5 unmodified lines
131 c.Writer.WriteHeaderNow()132 c.Abort()133}134135// AbortWithStatusJSON calls `Abort()` and then `JSON` internally. This method stops the chain, writes the status code and return a JSON body135// AbortWithStatusJSON calls `Abort()` and then `JSON` internally.136// This method stops the chain, writes the status code and return a JSON body.137// It also sets the Content-Type as "application/json".138func (c *Context) AbortWithStatusJSON(code int, jsonObj interface{}) {139 c.Abort()140 c.JSON(code, jsonObj)141}142142// AbortWithError calls `AbortWithStatus()` and `Error()` internally. This method stops the chain, writes the status code and143// pushes the specified error to `c.Errors`.143// AbortWithError calls `AbortWithStatus()` and `Error()` internally.144// This method stops the chain, writes the status code and pushes the specified error to `c.Errors`.145// See Context.Error() for more details.146func (c *Context) AbortWithError(code int, err error) *Error {147 c.AbortWithStatus(code)148 return c.Error(err)
4 unmodified lines
153/************************************/154155// Error attaches an error to the current context. The error is pushed to a list of errors.156// It's a good idea to call Error for each error that occurred during the resolution of a request.156// A middleware can be used to collect all the errors157// and push them to a database together, print a log, or append it in the HTTP response.157// A middleware can be used to collect all the errors and push them to a database together,158// print a log, or append it in the HTTP response.159// Error will panic if err is nil.160func (c *Context) Error(err error) *Error {161 if err == nil {162 panic("err is nil")
133 unmodified lines
296/************************************/297298// Param returns the value of the URL param.299// It is a shortcut for c.Params.ByName(key)299// router.GET("/user/:id", func(c *gin.Context) {300// // a GET request to /user/john301// id := c.Param("id") // id == "john"302// })300// router.GET("/user/:id", func(c *gin.Context) {301// // a GET request to /user/john302// id := c.Param("id") // id == "john"303// })304func (c *Context) Param(key string) string {305 return c.Params.ByName(key)306}307308// Query returns the keyed url query value if it exists,309// otherwise it returns an empty string `("")`.310// It is shortcut for `c.Request.URL.Query().Get(key)`310// GET /path?id=1234&name=Manu&value=311// c.Query("id") == "1234"312// c.Query("name") == "Manu"313// c.Query("value") == ""314// c.Query("wtf") == ""311// GET /path?id=1234&name=Manu&value=312// c.Query("id") == "1234"313// c.Query("name") == "Manu"314// c.Query("value") == ""315// c.Query("wtf") == ""316func (c *Context) Query(key string) string {317 value, _ := c.GetQuery(key)318 return value319}320321// DefaultQuery returns the keyed url query value if it exists,322// otherwise it returns the specified defaultValue string.323// See: Query() and GetQuery() for further information.323// GET /?name=Manu&lastname=324// c.DefaultQuery("name", "unknown") == "Manu"325// c.DefaultQuery("id", "none") == "none"326// c.DefaultQuery("lastname", "none") == ""324// GET /?name=Manu&lastname=325// c.DefaultQuery("name", "unknown") == "Manu"326// c.DefaultQuery("id", "none") == "none"327// c.DefaultQuery("lastname", "none") == ""328func (c *Context) DefaultQuery(key, defaultValue string) string {329 if value, ok := c.GetQuery(key); ok {330 return value331 }
3 unmodified lines
335// GetQuery is like Query(), it returns the keyed url query value336// if it exists `(value, true)` (even when the value is an empty string),337// otherwise it returns `("", false)`.338// It is shortcut for `c.Request.URL.Query().Get(key)`338// GET /?name=Manu&lastname=339// ("Manu", true) == c.GetQuery("name")340// ("", false) == c.GetQuery("id")341// ("", true) == c.GetQuery("lastname")339// GET /?name=Manu&lastname=340// ("Manu", true) == c.GetQuery("name")341// ("", false) == c.GetQuery("id")342// ("", true) == c.GetQuery("lastname")343func (c *Context) GetQuery(key string) (string, bool) {344 if values, ok := c.GetQueryArray(key); ok {345 return values[0], ok346 }
37 unmodified lines
384// GetPostForm is like PostForm(key). It returns the specified key from a POST urlencoded385// form or multipart form when it exists `(value, true)` (even when the value is an empty string),386// otherwise it returns ("", false).387// For example, during a PATCH request to update the user's email:387// email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"388// email= --> ("", true) := GetPostForm("email") // set email to ""389// --> ("", false) := GetPostForm("email") // do nothing with email388// email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"389// email= --> ("", true) := GetPostForm("email") // set email to ""390// --> ("", false) := GetPostForm("email") // do nothing with email391func (c *Context) GetPostForm(key string) (string, bool) {392 if values, ok := c.GetPostFormArray(key); ok {393 return values[0], ok394 }
37 unmodified lines
432}433434// Bind checks the Content-Type to select a binding engine automatically,435// Depending the "Content-Type" header different bindings are used:435// "application/json" --> JSON binding436// "application/xml" --> XML binding436// "application/json" --> JSON binding437// "application/xml" --> XML binding438// otherwise --> returns an error439// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.440// It decodes the json payload into the struct specified as a pointer.441// Like ParseBody() but this method also writes a 400 error if the json is not valid.
335 unmodified lines