国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

高端做網(wǎng)站哪家好臨沂seo

高端做網(wǎng)站哪家好,臨沂seo,大牌網(wǎng)站設(shè)計,使用c 語言建設(shè)網(wǎng)站優(yōu)點學(xué)習(xí)記錄 1 集成 Gorilla Mux1.1 為什么不選擇 HttpRouter?1.2 安裝 gorilla/mux1.3 使用 gorilla/mux1.4 遷移到 Gorilla Mux1.4.1 新增 homeHandler1.4.2 指定 Methods () 來區(qū)分請求方法1.4.3 請求路徑參數(shù)和正則匹配1.4.4 命名路由與鏈接生成 1 集成 Gorilla Mu…

學(xué)習(xí)記錄

  • 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 () 來區(qū)分請求方法
      • 1.4.3 請求路徑參數(shù)和正則匹配
      • 1.4.4 命名路由與鏈接生成

1 集成 Gorilla Mux

1.1 為什么不選擇 HttpRouter?

HttpRouter是目前最快的路由器,被知名GIN框架所采用。
沒有選擇HttpRouter是因為功能略顯單一,沒有由于命名功能。
HttpRouter和GIN比較適合性能要求高,路由功能相對簡單的項目中,如API或微服務(wù)。在全站開發(fā)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, "此博客是用以記錄編程筆記,如您有反饋或建議,請聯(lián)系 "+"<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>如有疑惑,請聯(lián)系我們。</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, "創(chuàng)建新的文章")
}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 () 來區(qū)分請求方法

在這里插入圖片描述
curl測試

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

注意: 在 Gorilla Mux 中,如未指定請求方法,默認會匹配所有方法。

1.4.3 請求路徑參數(shù)和正則匹配

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

在這里插入圖片描述
Handler 中獲取到這個參數(shù):

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

1.4.4 命名路由與鏈接生成

在這里插入圖片描述
air中顯示打印結(jié)果
在這里插入圖片描述


http://aloenet.com.cn/news/29362.html

相關(guān)文章:

  • 人大兩學(xué)一做專題網(wǎng)站深圳營銷型網(wǎng)站開發(fā)
  • 建應(yīng)用網(wǎng)站百度pc網(wǎng)頁版
  • 網(wǎng)站格式圖片全國唯一一個沒有疫情的城市
  • 杭州 網(wǎng)站建設(shè)seo引擎優(yōu)化方案
  • 淘寶客網(wǎng)站怎么做推廣windows優(yōu)化大師要會員
  • 無錫大型網(wǎng)站建設(shè)公司公司網(wǎng)頁制作需要多少錢
  • 經(jīng)營閱讀網(wǎng)站需要怎么做排名優(yōu)化方法
  • 建設(shè)政府網(wǎng)站的目的意義百度推廣每年600元什么費用
  • 做調(diào)查賺錢的網(wǎng)站有哪些南寧優(yōu)化網(wǎng)站收費
  • 種植園網(wǎng)站模板無錫百度正規(guī)推廣
  • 桂林做網(wǎng)站的公司全國疫情最新數(shù)據(jù)
  • 計算機做網(wǎng)站開發(fā)需要什么證書seo這個職位是干什么的
  • 靈感集網(wǎng)站網(wǎng)絡(luò)營銷概念
  • 北京城鄉(xiāng)和住房建設(shè)部網(wǎng)站seo專員是干什么的
  • 網(wǎng)站建設(shè)開發(fā)能力很強的企業(yè)外貿(mào)公司一般怎么找客戶
  • ae素材網(wǎng)關(guān)鍵詞是網(wǎng)站seo的核心工作
  • 千助網(wǎng)站公司免費招收手游代理
  • 重慶付費網(wǎng)站推廣友情鏈接交換工具
  • 來賓市住房和城鄉(xiāng)建設(shè)局網(wǎng)站網(wǎng)站運營師
  • dnf做漢堡怎么玩間網(wǎng)站怎么做好網(wǎng)絡(luò)推廣銷售
  • 做網(wǎng)站運營還是翻譯如何自己弄個免費網(wǎng)站
  • 網(wǎng)頁版工具沈陽seo排名優(yōu)化教程
  • 響應(yīng)式設(shè)計網(wǎng)站網(wǎng)絡(luò)營銷的五大特點
  • 有哪些免費做電子名片的網(wǎng)站seo經(jīng)驗
  • 分紅網(wǎng)站建設(shè)網(wǎng)上營銷新觀察網(wǎng)
  • 網(wǎng)站二次開發(fā)模板搜索引擎廣告推廣
  • php如何做局域網(wǎng)的網(wǎng)站外貿(mào)網(wǎng)站谷歌seo
  • wordpress賬號注冊機臨沂seo顧問
  • 做設(shè)計在哪個網(wǎng)站找圖片選擇寧波seo優(yōu)化公司
  • python做網(wǎng)站實例最新中央人事任免