mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
Merge c78c27d8c21861fd619fe233a85f3210a49b9e7f into 292f1ec6df050cbd701eb4eb217d5ba2f57e8681
This commit is contained in:
commit
019a498ed4
15
context.go
15
context.go
@ -8,11 +8,12 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin/binding"
|
"github.com/gin-gonic/gin/binding"
|
||||||
"github.com/gin-gonic/gin/render"
|
"github.com/gin-gonic/gin/render"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -187,6 +188,16 @@ func (c *Context) MustGet(key string) interface{} {
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDefault returns the value for the given key or a default value if the key does not exist.
|
||||||
|
func (c *Context) GetDefault(key string, defaultVal interface{}) interface{} {
|
||||||
|
item, err := c.Get(key)
|
||||||
|
if err != nil {
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
|
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
/******** ENCOGING MANAGEMENT********/
|
/******** ENCOGING MANAGEMENT********/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
@ -54,6 +54,17 @@ func TestContextSetGet(t *testing.T) {
|
|||||||
if v != "bar" {
|
if v != "bar" {
|
||||||
t.Errorf("Value should be bar, was %s", v)
|
t.Errorf("Value should be bar, was %s", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDefault
|
||||||
|
v = c.GetDefault("foo", "baz")
|
||||||
|
if v != "bar" {
|
||||||
|
t.Errorf("Value should be bar, was %s", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
v = c.GetDefault("badKey", "baz")
|
||||||
|
if v != "baz" {
|
||||||
|
t.Errorf("Value should be baz, was %s", v)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
r.ServeHTTP(w, req)
|
r.ServeHTTP(w, req)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user