当前位置: 首页 > news >正文

高端做网站哪家好百度搜索排行

高端做网站哪家好,百度搜索排行,个人博客登录首页,扬州中兴建设有限公司网站学习记录 1 集成 Gorilla Mux1.1 为什么不选择 HttpRouter?1.2 安装 gorilla/mux1.3 使用 gorilla/mux1.4 迁移到 Gorilla Mux1.4.1 新增 homeHandler1.4.2 指定 Methods () 来区分请求方法1.4.3 请求路径参数和正则匹配1.4.4 命名路由与链接生成 1 集成 Gorilla Mu…

学习记录

  • 1 集成 Gorilla Mux
    • 1.1 为什么不选择 HttpRouter?
    • 1.2 安装 gorilla/mux
    • 1.3 使用 gorilla/mux
    • 1.4 迁移到 Gorilla Mux
      • 1.4.1 新增 homeHandler
      • 1.4.2 指定 Methods () 来区分请求方法
      • 1.4.3 请求路径参数和正则匹配
      • 1.4.4 命名路由与链接生成

1 集成 Gorilla Mux

1.1 为什么不选择 HttpRouter?

HttpRouter是目前最快的路由器,被知名GIN框架所采用。
没有选择HttpRouter是因为功能略显单一,没有由于命名功能。
HttpRouter和GIN比较适合性能要求高,路由功能相对简单的项目中,如API或微服务。在全站开发Web中,gorilla/mux功能更强大,比较实用。

1.2 安装 gorilla/mux

安装第三方依赖

go get -u github.com/gorilla/mux

查看文件变更情况

git status

两个文件变更
在这里插入图片描述

1.3 使用 gorilla/mux

在这里插入图片描述
在这里插入图片描述

1.4 迁移到 Gorilla Mux

改进main.go

package mainimport ("fmt""net/http""github.com/gorilla/mux"
)func homeHandler(w http.ResponseWriter, r *http.Request) {w.Header().Set("Content-Type", "text/html; charset=utf-8")fmt.Fprint(w, "<h1>Hello, 欢迎来到 goblog!</h1>")
}func aboutHandler(w http.ResponseWriter, r *http.Request) {w.Header().Set("Content-Type", "text/html; charset=utf-8")fmt.Fprint(w, "此博客是用以记录编程笔记,如您有反馈或建议,请联系 "+"<a href=\"mailto:summer@example.com\">summer@example.com</a>")
}func notFoundHandler(w http.ResponseWriter, r *http.Request) {w.Header().Set("Content-Type", "text/html; charset=utf-8")w.WriteHeader(http.StatusNotFound)fmt.Fprint(w, "<h1>请求页面未找到 :(</h1><p>如有疑惑,请联系我们。</p>")
}func articlesShowHandler(w http.ResponseWriter, r *http.Request) {vars := mux.Vars(r)id := vars["id"]fmt.Fprint(w, "文章 ID:"+id)
}func articlesIndexHandler(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w, "访问文章列表")
}func articlesStoreHandler(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w, "创建新的文章")
}func main() {router := mux.NewRouter()router.HandleFunc("/", homeHandler).Methods("GET").Name("home")router.HandleFunc("/about", aboutHandler).Methods("GET").Name("about")router.HandleFunc("/articles/{id:[0-9]+}", articlesShowHandler).Methods("GET").Name("articles.show")router.HandleFunc("/articles", articlesIndexHandler).Methods("GET").Name("articles.index")router.HandleFunc("/articles", articlesStoreHandler).Methods("POST").Name("articles.store")// 自定义 404 页面router.NotFoundHandler = http.HandlerFunc(notFoundHandler)// 通过命名路由获取 URL 示例homeURL, _ := router.Get("home").URL()fmt.Println("homeURL: ", homeURL)articleURL, _ := router.Get("articles.show").URL("id", "23")fmt.Println("articleURL: ", articleURL)http.ListenAndServe(":3000", router)
}

1.4.1 新增 homeHandler

首先,因为使用的是精确匹配,我们将 defaultHandler 变更 homeHandler 且将处理 404 的代码移除。

1.4.2 指定 Methods () 来区分请求方法

在这里插入图片描述
curl测试

curl http://localhost:3000/articles
curl -Method POST http://localhost:3000/articles

注意: 在 Gorilla Mux 中,如未指定请求方法,默认会匹配所有方法。

1.4.3 请求路径参数和正则匹配

router.HandleFunc("/articles/{id:[0-9]+}", articlesShowHandler).Methods("GET").Name("articles.show")

在这里插入图片描述
Handler 中获取到这个参数:

func articlesShowHandler(w http.ResponseWriter, r *http.Request) {vars := mux.Vars(r)id := vars["id"]fmt.Fprint(w, "文章 ID:"+id)
}

1.4.4 命名路由与链接生成

在这里插入图片描述
air中显示打印结果
在这里插入图片描述


http://www.mnyf.cn/news/34039.html

相关文章:

  • 网站建设 山西软文代发平台
  • 做网站前期ps 图多大百度浏览器官方网站
  • 那些空号检测网站是怎么做的阿里云建站费用
  • 沈阳怎么做网站网络营销项目策划书
  • apicloud成品源码长沙官网seo收费
  • 潍坊做网站的那家好流量宝官网
  • 济南建设网点电话网站排名优化公司哪家好
  • 商城网站要怎样建设如何做网站推广的策略
  • 内蒙古建信建设有限公司网站优化大师官方免费
  • mip wordpress模板seo怎么做优化方案
  • 做电影网站赚钱的方法济南最新消息
  • o2o网站建设互联网广告投放公司
  • 公众号电影网站怎么做济宁seo推广
  • 网站图片自动切换怎么做女生学市场营销好吗
  • 做爰午夜福利全过程视频网站网络平台推广
  • 加强统筹推进政府网站建设百度seo优化方法
  • 规模大的企业建站网络营销型网站
  • 东莞最近有什么新闻站长工具seo综合查询columbu cat
  • 制作网站哪家专业优化教程网官网
  • 建设银行档案管理网站淘宝宝贝关键词排名查询工具
  • 做受免费网站手机建站教程
  • 网站建设报价表格式新公司如何做推广
  • 个人网站自助建站搜seo
  • 怎样让公司网站更吸引人免费广告推广软件
  • 南昌网站建设哪家好薇福州模板建站哪家好
  • 男女生做恶心的网站关键词排名 收录 查询
  • 电脑网站滚动字幕怎么做免费收录网站推广
  • 网站关键字没有排名谷歌seo网站推广
  • 色块的网站seo优化推广专员招聘
  • 企业网站建设费用 珠海主流搜索引擎有哪些