## Context
echo.Context 表示當前 HTTP 請求的上下文。通過路徑、路徑參數、數據、注冊處理器和相關 API 進行請求的讀取與響應的輸出。由于 Context 是一個接口,也可以輕松地使用自定義 API 進行擴展。
### 擴展 Context
**定義一個自定義 context**
```go
type CustomContext struct {
echo.Context
}
func (c *CustomContext) Foo() {
println("foo")
}
func (c *CustomContext) Bar() {
println("bar")
}
```
**創建一個中間件來擴展默認的 context**
```go
e.Use(func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
cc := &CustomContext{c}
return h(cc)
}
})
```
> 此中間件應在任何其他中間件之前注冊。
**在處理器中使用**
```go
e.Get("/", func(c echo.Context) error {
cc := c.(*CustomContext)
cc.Foo()
cc.Bar()
return cc.String(200, "OK")
})
```