zoukankan      html  css  js  c++  java
  • 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()
    }
  • 相关阅读:
    链表的常用操作
    android简易论坛的制作
    Bmob后端云的使用
    马哥第九周
    马哥第八周
    马哥第七周
    马哥第六周
    马哥第五周
    马哥第四周
    马哥第三周
  • 原文地址:https://www.cnblogs.com/wangjq19920210/p/11583646.html
Copyright © 2011-2022 走看看