Improves documentation

This commit is contained in:
Manu Mtz.-Almeida 2016-01-26 22:40:29 +01:00
parent e9531e5c76
commit 61fae4997d
4 changed files with 22 additions and 21 deletions

View File

@ -185,7 +185,8 @@ func (c *Context) MustGet(key string) interface{} {
// Query is a shortcut for c.Request.URL.Query().Get(key) // Query is a shortcut for c.Request.URL.Query().Get(key)
// It is used to return the url query values. // It is used to return the url query values.
// ?id=1234&name=Manu // It returns an empty string ("") when the value does not exist.
// /path?id=1234&name=Manu
// c.Query("id") == "1234" // c.Query("id") == "1234"
// c.Query("name") == "Manu" // c.Query("name") == "Manu"
// c.Query("wtf") == "" // c.Query("wtf") == ""
@ -195,12 +196,18 @@ func (c *Context) Query(key string) string {
} }
// PostForm is a shortcut for c.Request.PostFormValue(key) // PostForm is a shortcut for c.Request.PostFormValue(key)
// It returns an empty string ("") when the value does not exist.
func (c *Context) PostForm(key string) string { func (c *Context) PostForm(key string) string {
value, _ := c.postForm(key) value, _ := c.postForm(key)
return value return value
} }
// Param is a shortcut for c.Params.ByName(key) // Param returns the value of the URL param.
// It is a shortcut for c.Params.ByName(key)
// router.GET("/user/:id", func(c *gin.Context) {
// // a GET request to /user/john
// id := c.Param("id") // id == "john"
// })
func (c *Context) Param(key string) string { func (c *Context) Param(key string) string {
return c.Params.ByName(key) return c.Params.ByName(key)
} }
@ -214,11 +221,9 @@ func (c *Context) DefaultPostForm(key, defaultValue string) string {
// DefaultQuery returns the keyed url query value if it exists, othewise it returns the // DefaultQuery returns the keyed url query value if it exists, othewise it returns the
// specified defaultValue. // specified defaultValue.
// ``` // //?name=Manu
// /?name=Manu // c.DefaultQuery("name", "unknown") == "Manu"
// c.DefaultQuery("name", "unknown") == "Manu" // c.DefaultQuery("id", "none") == "none"
// c.DefaultQuery("id", "none") == "none"
// ```
func (c *Context) DefaultQuery(key, defaultValue string) string { func (c *Context) DefaultQuery(key, defaultValue string) string {
if value, ok := c.query(key); ok { if value, ok := c.query(key); ok {
return value return value
@ -250,8 +255,8 @@ func (c *Context) postForm(key string) (string, bool) {
// Bind checks the Content-Type to select a binding engine automatically, // Bind checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used: // Depending the "Content-Type" header different bindings are used:
// "application/json" --> JSON binding // "application/json" --> JSON binding
// "application/xml" --> XML binding // "application/xml" --> XML binding
// otherwise --> returns an error // otherwise --> returns an error
// If Parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. // If Parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
// It decodes the json payload into the struct specified as a pointer. // It decodes the json payload into the struct specified as a pointer.

View File

@ -109,13 +109,11 @@ func (a errorMsgs) Last() *Error {
} }
// Returns an array will all the error messages. // Returns an array will all the error messages.
// Example // Example:
// ``` // c.Error(errors.New("first"))
// c.Error(errors.New("first")) // c.Error(errors.New("second"))
// c.Error(errors.New("second")) // c.Error(errors.New("third"))
// c.Error(errors.New("third")) // c.Errors.Errors() // == []string{"first", "second", "third"}
// c.Errors.Errors() // == []string{"first", "second", "third"}
// ``
func (a errorMsgs) Errors() []string { func (a errorMsgs) Errors() []string {
if len(a) == 0 { if len(a) == 0 {
return nil return nil

View File

@ -29,10 +29,8 @@ const (
// Note that both Logger and Recovery provides custom ways to configure their // Note that both Logger and Recovery provides custom ways to configure their
// output io.Writer. // output io.Writer.
// To support coloring in Windows use: // To support coloring in Windows use:
// ``` // import "github.com/mattn/go-colorable"
// import "github.com/mattn/go-colorable" // gin.DefaultWriter = colorable.NewColorableStdout()
// gin.DefaultWriter = colorable.NewColorableStdout()
// ```
var DefaultWriter io.Writer = os.Stdout var DefaultWriter io.Writer = os.Stdout
var DefaultErrorWriter io.Writer = os.Stderr var DefaultErrorWriter io.Writer = os.Stderr

View File

@ -23,7 +23,7 @@ var (
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. // Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
func Recovery() HandlerFunc { func Recovery() HandlerFunc {
return RecoveryWithWriter(DefaultWriter) return RecoveryWithWriter(DefaultErrorWriter)
} }
func RecoveryWithWriter(out io.Writer) HandlerFunc { func RecoveryWithWriter(out io.Writer) HandlerFunc {