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

高端做网站哪家好临沂seo

高端做网站哪家好,临沂seo,大牌网站设计,使用c 语言建设网站优点学习记录 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.wooajung.com/news/23702.html

相关文章:

  • 深圳创业补贴政策2022申请条件网站排名优化制作
  • 网页设计实训报告总结100字北京seo优化推广
  • 利用切片做网站背景图片域名访问网站怎么进入
  • 闸北专业做网站网络推广外包公司干什么的
  • 郑州网站建设最低价网络营销运营方案
  • 预约做家庭清洁的网站西安网站建设制作公司
  • 怎么制作网站教程图片石家庄seo按天扣费
  • 织梦网站优化教程开个网站平台要多少钱
  • wordpress主题官网seo关键词大搜
  • 搜索引擎优化教程seo词条
  • 东莞机械建站如何培训机构网站
  • 做调查赚钱的网站有哪些南宁优化网站收费
  • 中国企业500强搜索版宜昌网站seo收费
  • 哪里做网站排名职业培训机构
  • 网站 多线域名注册好了怎么弄网站
  • 寻找郑州网站建设网站制作维护
  • 网站建设内存网络推广营销策划方案
  • 网站产品动效怎么做最新最好的磁力搜索
  • 做新闻网站怎么赚钱北京网站排名seo
  • 做像淘宝这样的购物网站要多少钱佛山本地网站建设
  • 网站建设规划快速将网站seo
  • 给别人做网站怎么收取费用域名被墙查询
  • 游戏服务器租用多少钱一年郑州seo公司排名
  • 百度给做网站公司网站如何优化关键词排名
  • 千助网站公司免费招收手游代理
  • 互联网站建设维护是做什么的廊坊自动seo
  • 做网站前期了解客户什么需求沈阳网站seo排名公司
  • 可以做产品宣传的网站seo服务价格表
  • 网站和app可以做充值余额功能如何做百度关键词推广
  • 如何做织梦论坛类的网站长春网络推广公司哪个好