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

【go项目01_学习记录04】

学习记录

  • 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.lryc.cn/news/344286.html

相关文章:

  • HCIP第二节
  • Ubuntu MATE系统下WPS显示错位
  • Mysql进阶-索引篇
  • 【算法系列】哈希表
  • Git推送本地项目到gitee远程仓库
  • 一键复制:基于vue实现的tab切换效果
  • 新手做抖音小店,卖什么最容易出单?抖音必爆类目来了!
  • 男人圣经 10
  • 如何让路由器分配固定网段(网络号)ip
  • Q1保健品线上市场分析(三):牛初乳市场扩张,同比去年增长54%
  • 使用docker-compose编排Lnmp(dockerfile) 完成Wordpress
  • 母婴店运用商城小程序店铺的效果是什么
  • 大数据技术概述_2.大数据面临的5个方面的挑战
  • 《动手学深度学习(Pytorch版)》Task03:线性神经网络——4.29打卡
  • 机器学习(二) ----------K近邻算法(KNN)+特征预处理+交叉验证网格搜索
  • This error originates from a subprocess, and is likely not a problem with pip.
  • Python中关于子类约束的开发规范
  • Isaac Sim 4 键盘控制小车前进方向(学习笔记5.8.2)
  • ​「Python绘图」绘制太极图
  • 解决html2canvas生成图片慢的问题
  • 模型智能体开发之metagpt-多智能体实践
  • Java | Leetcode Java题解之第67题二进制求和
  • 考过PMP之后,为什么建议学CSPM?
  • 智能合约是什么?搭建与解析
  • windows下安装最新的nginx
  • 【深耕 Python】Data Science with Python 数据科学(19)书402页练习题:模型准确率对比研究、KMeans算法的一点探讨
  • 汽车品牌区域营销方案
  • matlab 中在3维坐标系中绘制一个点的X,Y,Z坐标,除了mesh还有什么函数?使用格式与mesh都有什么区别?
  • 如何在六个月内学会任何一门外语(ted转述)
  • 前端 Android App 上架详细流程 (Android App)