https://img-blog.csdnimg.cn/32db9ce43ef64316a2e37a31f4cee033.gif
编程小鱼酱yu612.com,点击前往

02.go搭建一个web服务器


接收get、post消息


浏览器测试地址 http://localhost:9000/login


package main

import (

    "fmt"

    "net/http"

)

func login(w http.ResponseWriter, r *http.Request) {

    r.ParseForm()

    fmt.Println(r.Method)

    if r.Method == "GET" 

    {

        fmt.Fprintf(w, "This is a GET request")

    } 

    else if r.Method == "POST" 

    {

        fmt.Fprintf(w, "This is a POST request")

    }

    else {

        w.Header().Set("Access-Control-Allow-Origin", "*")

        fmt.Println("Recived info:", r.Form)

        fmt.Fprintf(w, r.Form.Get("info"))

    }

}

func main() {

    http.HandleFunc("/login", login)

    if err := http.ListenAndServe(":9000", nil); err != nil {

        fmt.Println("ListenAndServe err", err)

    }

}



posted @ 2017-12-28 11:07  鱼酱  阅读(101)  评论(0编辑  收藏  举报

https://img-blog.csdnimg.cn/32db9ce43ef64316a2e37a31f4cee033.gif
编程小鱼酱yu612.com,点击前往