go实现http的请求方式

1.默认的常规方法

//默认多路复用器
import (
	"fmt"
	"net/http"
)
func IndexHand (w http.ResponseWriter,r *http.Request) {
	content:="this is test info"
	fmt.Fprint(w,content)
}
func main(){
	http.HandleFunc("/test",IndexHand)
	http.ListenAndServe("127.0.0.1:8005",nil)
}

 2.多路复用器

//个性化多路复用器
func IndexFucn(w http.ResponseWriter,r * http.Request) {
	content:="this is content"
	fmt.Fprint(w,content)
}
func secondFun(w http.ResponseWriter,r * http.Request){
	content:="this is second"
	fmt.Fprint(w,content)
}
func  main(){
	mux:=http.NewServeMux()
	mux.HandleFunc("/aa",IndexFucn)
	mux.HandleFunc("/bb",secondFun)
	server:=&http.Server{
		Addr: "0.0.0.0:8099",
		Handler: mux,
	}
	err:=server.ListenAndServe()
	if err!=nil {
		log.Fatal(err)
	}
}

  

原文地址:https://www.cnblogs.com/zh718594493/p/15338674.html