gorilla/mux 的学习

原文链接:gorilla/mux的学习

源代码:

package main

import (
    "encoding/json"
    "fmt"
    "github.com/gorilla/mux"
    "io/ioutil"
    "net/http"
    "net/url"
    "time"
)

//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]string
    json.Unmarshal(body, &params)
    fmt.Println(params["msg"])
    resp := "hello" + params["msg"]
    w.Write([]byte(resp))
}

func main() {
    Router := mux.NewRouter()
    //配置路由
    Router.HandleFunc("/hello", hello).Methods("GET") //可以不定参
    Router.HandleFunc("/hello1", hello1).Methods("POST")

    //设置端口 路由
    server := http.Server{
        Addr:         ":11111",
        ReadTimeout:  time.Second,
        WriteTimeout: time.Second,
        Handler:      Router,
    }
    //启动监听
    server.ListenAndServe()
}

 

posted @ 2019-09-25 11:32  salami_china  阅读(287)  评论(0编辑  收藏  举报