package mainimport ("github.com/gorilla/mux""net/http""net/url""fmt""time""io/ioutil""encoding/json"
)//var Router *mux.Routerfunc main() {Router := mux.NewRouter()//配置路由Router.HandleFunc("/hello",hello).Methods("GET") //可以不定参Router.HandleFunc("/hello1",hello1).Methods("POST")//设置端口 路由server := http.Server{Addr:":8080",ReadTimeout:time.Second,WriteTimeout:time.Second,Handler:Router,}//启动监听server.ListenAndServe()
}//get
func hello(w http.ResponseWriter,r *http.Request) {//参数解析params,_ := url.ParseQuery(r.URL.RawQuery)msg := "hello"+params["msg"][0]fmt.Println(msg)w.Write([]byte(msg))
}//post
func hello1(w http.ResponseWriter,r *http.Request) {//参数解析body,_ := ioutil.ReadAll(r.Body)var params map[string]stringjson.Unmarshal(body, ¶ms)fmt.Println(params["msg"])resp := "hello" + params["msg"]w.Write([]byte(resp))
}
结果