您的位置:首页 > 编程语言 > Go语言

02.go搭建一个web服务器

2017-12-28 11:07 549 查看
接收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)

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: