Go·接收Get请求

Go·接收Get请求

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	//解决静态文件访问 404 ,通过/upload/文件夹./upload里的内容
	http.Handle("/upload/", http.FileServer(http.Dir("./upload")))

	//访问服务器目录./public文件夹里的内容,可以通过 http://localhost:3000/static来进行访问。
	//http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./upload"))))

	http.HandleFunc("/get", Getaaa)

	err := http.ListenAndServe(":9999", nil) //监听端口
	if err != nil {
		log.Fatal("监听并发: ", err.Error())
	}

}

// 接收请求
func Getaaa(writer http.ResponseWriter, request *http.Request) {

	query := request.URL.Query()

	// 第一种方式
	// id := query["id"][0]

	// 第二种方式

	id := query.Get("id")

	log.Printf("GET: id=%s\n", id)

	fmt.Fprintf(writer, `{"code":0}`) //返回对象
	log.Println("123456789")
}
433 Views
分享你的喜爱
linwute
linwute

我要像梦一样自由,像大地一样宽容;
在艰辛放逐的路上,点亮生命的光芒;
我要像梦一样自由,像天空一样坚强;
在曲折蜿蜒的路上,体验生命的意义;

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注