package main //gohttp底层实现,通过gohttp不是通过beego实现的输出 // import ( "io" "log" "net/http" ) func main() { //设置路由 http.HandleFunc("/", sayHello) err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal(err) } } func sayHello(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello vwesion1") }
package main //gohttp第二个版本:通过mux,handler实现路由 import ( "io" "log" "net/http" ) func main() { mux := http.NewServeMux() //ServeMux//实现handler注册到ServeMux然后在进行路由的注册 mux.Handle("/", &myHandler{}) //注册路由和handler mux.Handle("/hello", sayHello) //通过函数注册路由 err := http.ListenAndServe(":8080", mux) if err != nil { log.Fatal(err) } } type myHandler struct { //这个handle要实现ServerHTTP方法 } func (this *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello vwesion2,"+r.URL.String()) } func sayHello(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello vwesion2,"+r.URL.String()) }