和拓者设计吧类似的网站,济南网站排名推广,网站重要性,建设网站只到这里前七天的任务已经完成#xff0c;但我们可以对Gee框架进行一些扩展
补充HTTP请求方法
原作者只实现了 GET, POST 路由添加#xff0c;其他的 PUT, DELETE 等标准 HTTP 方法未实现#xff0c;实现方法也很简单#xff0c;只需在gee.go中增加如下代码
// PUT define…到这里前七天的任务已经完成但我们可以对Gee框架进行一些扩展
补充HTTP请求方法
原作者只实现了 GET, POST 路由添加其他的 PUT, DELETE 等标准 HTTP 方法未实现实现方法也很简单只需在gee.go中增加如下代码
// PUT defines the method to add PUT request
func (group *RouterGroup) PUT(pattern string, handler HandlerFunc) {group.addRoute(http.MethodPut, pattern, handler)
}// DELETE defines the method to add DELETE request
func (group *RouterGroup) DELETE(pattern string, handler HandlerFunc) {group.addRoute(http.MethodDelete, pattern, handler)
}// PATCH defines the method to add PATCH request
func (group *RouterGroup) PATCH(pattern string, handler HandlerFunc) {group.addRoute(http.MethodPatch, pattern, handler)
}// HEAD defines the method to add HEAD request
func (group *RouterGroup) HEAD(pattern string, handler HandlerFunc) {group.addRoute(http.MethodHead, pattern, handler)
}// OPTIONS defines the method to add OPTIONS request
func (group *RouterGroup) OPTIONS(pattern string, handler HandlerFunc) {group.addRoute(http.MethodOptions, pattern, handler)
}// TRACE defines the method to add TRACE request
func (group *RouterGroup) TRACE(pattern string, handler HandlerFunc) {group.addRoute(http.MethodTrace, pattern, handler)
}// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.
func (group *RouterGroup) Any(pattern string, handler HandlerFunc) {group.GET(pattern, handler)group.POST(pattern, handler)group.PUT(pattern, handler)group.DELETE(pattern, handler)group.PATCH(pattern, handler)group.HEAD(pattern, handler)group.OPTIONS(pattern, handler)group.TRACE(pattern, handler)
}
参考gin实现Cookie
在context.go文件中加入以下代码
// SetCookie adds a Set-Cookie header to the ResponseWriters headers.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {if path {path /}http.SetCookie(c.Writer, http.Cookie{Name: name,Value: url.QueryEscape(value),MaxAge: maxAge,Path: path,Domain: domain,Secure: secure,HttpOnly: httpOnly,})
}// Cookie returns the named cookie provided in the request or
// ErrNoCookie if not found. And return the named cookie is unescaped.
// If multiple cookies match the given name, only one cookie will
// be returned.
func (c *Context) Cookie(name string) (string, error) {cookie, err : c.Req.Cookie(name)if err ! nil {return , err}val, _ : url.QueryUnescape(cookie.Value)return val, nil
}使用方法
r.GET(/setcookie, func(c *gee.Context) {c.SetCookie(gee_cookie, gee_cookie, 3600, /, localhost, false, true)c.String(http.StatusOK, set cookie: gee_cookie)
})r.GET(/getcookie, func(c *gee.Context) {cookie, _ : c.Cookie(gee_cookie)c.String(http.StatusOK, get cookie: %s, cookie)
})