go mux 实现http路由

github地址

https://gitee.com/mirrors/mux#examples

参考代码

package main

import (
	"fmt"
	"net/http"
	"github.com/gorilla/mux"
)

func main() {

	// IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headers
	r := mux.NewRouter()
	r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
	http.Handle("/", r)

	http.ListenAndServe(":8080", r)
}

func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	w.WriteHeader(http.StatusOK)
	fmt.Fprintf(w, "Category: %v\n", vars["category"])
}

go run main.php
浏览器访问 http://localhost:8080/articles/werwer/

这个包只是路由分发,http访问还是要自带的http

posted @ 2020-10-21 12:33  brady-wang  阅读(690)  评论(0编辑  收藏  举报