mirror of https://github.com/gin-gonic/gin.git
Adding MustGet method. Updating README.
This commit is contained in:
parent
108bfb4470
commit
abe076b8f8
|
@ -294,7 +294,7 @@ func main() {
|
||||||
r.Use(Logger())
|
r.Use(Logger())
|
||||||
|
|
||||||
r.GET("/test", func(c *gin.Context){
|
r.GET("/test", func(c *gin.Context){
|
||||||
example := r.Get("example").(string)
|
example := c.MustGet("example").(string)
|
||||||
|
|
||||||
// it would print: "12345"
|
// it would print: "12345"
|
||||||
log.Println(example)
|
log.Println(example)
|
||||||
|
|
13
gin.go
13
gin.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
@ -280,8 +281,7 @@ func (c *Context) Set(key string, item interface{}) {
|
||||||
c.Keys[key] = item
|
c.Keys[key] = item
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value for the given key.
|
// Get returns the value for the given key or an error if the key does not exist.
|
||||||
// It panics if the value doesn't exist.
|
|
||||||
func (c *Context) Get(key string) (interface{}, error) {
|
func (c *Context) Get(key string) (interface{}, error) {
|
||||||
if c.Keys != nil {
|
if c.Keys != nil {
|
||||||
item, ok := c.Keys[key]
|
item, ok := c.Keys[key]
|
||||||
|
@ -292,6 +292,15 @@ func (c *Context) Get(key string) (interface{}, error) {
|
||||||
return nil, errors.New("Key does not exist.")
|
return nil, errors.New("Key does not exist.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MustGet returns the value for the given key or panics if the value doesn't exist.
|
||||||
|
func (c *Context) MustGet(key string) interface{} {
|
||||||
|
value, err := c.Get(key)
|
||||||
|
if err != nil || value == nil {
|
||||||
|
log.Panicf("Key %s doesn't exist", key)
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
/******** ENCOGING MANAGEMENT********/
|
/******** ENCOGING MANAGEMENT********/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
|
Loading…
Reference in New Issue