- Simple use of HTTP Handlefunc
package mainimport ( "log" "net/http")func main() { //注册一个函数,响应某一个路由 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello this is version 1!!")) }) //这里可以单独写一个函数传递给当前的路由 http.HandleFunc("/bye", SayBye) log.Println("Start version v1") log.Fatal(http.ListenAndServe(":4000", nil))}func SayBye(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Bye bye, this is version v1")) //进行一个流式传递,将字符串转换为byte类型}
- Define the MUX yourself and use
package mainimport ( "log" "net/http")func main() { mux := http.NewServeMux() //自己创建servemux,然后使用自己的handle方法 mux.Handle("/", &myHandler{}) //默认的mux中根路由包含了所有的未匹配的路由 mux.HandleFunc("/bye", SayBye) log.Println("Start version v1") log.Fatal(http.ListenAndServe(":4000", mux))}type myHandler struct{} //自己定义handler结构//实现myHandler的ServeHTPP方法func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello this is version 2!,the requeset URL is:" + r.URL.String())) //这里可以打印出完整的URL,响应的都是根路由 //大部分web结构的路由都是在ServerHTTP的方法中实现的}func SayBye(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Bye bye, this is version v2")) //进行一个流式传递,将字符串转换为byte类型}
Source parsing func Listenandserve (addr string, handler handler) error {server: = &server{addr:addr, Handler:handler} RE Turn server. Listenandserve ()}//Here you can see that we can customize the server object and then listen//Full source package Mainimport ("Log" "Net/http" "Time") Func main () { Server: = &http. server{Addr: ": 4000", Writetimeout:4 * time. Second,} MUX: = http. Newservemux ()///create Servemux yourself and use your own handle method, the MUX is a variable mux that implements the handler interface. Handle ("/", &myhandler{})//The root route in the default MUX contains all the unmatched route MUX. Handlefunc ("/bye", Saybye)//MUX is integrated into server, server. Handle is also an interface of the handle type, so you can assign the server directly. Handler = MUX Log. Println ("Start version v3") log. Fatal (server. Listenandserve ())}type MyHandler struct{}//self-defined handler structure//MyHandler method for Implementing SERVEHTPP func (*myhandler) servehttp (w http . Responsewriter, R *http. Request) {w.write ("]byte" ("Hello" is version 3!,the Requeset URL is: "+ r.url. String ()))//The complete URL can be printed here, in response to the root route}func Saybye (w http. Responsewriter, R *http. Request) { Time. Sleep (3 * time. Second) W.write ([]byte ("Bye Bye, this is version V3"))//Make a streaming pass, convert the string to a byte type}
New features of the
- GO1.8 that allow active stopping of HTTP servers
Package Mainimport ("Log" "Net/http" "OS" "Os/signal" "Time") Func main () {server: = &http. server{Addr: ": 4000", Writetimeout:4 * time. Second,} Quit: = Make (chan os. Signal)//Create Chan, to instruct me to quit this server, please help close Signal. Notify (quit, OS. INTERRUPT)//Register this notification event, once received by this singal, send an object to this Chan, when I receive an arbitrary object, I know that the server should exit the MUX: = http. Newservemux ()///create Servemux yourself and use your own handle method, the MUX is a variable mux that implements the handler interface. Handle ("/", &myhandler{})//The root route in the default MUX contains all the unmatched route MUX. Handlefunc ("/bye", Saybye)//MUX is integrated into server, server. Handle is also an interface of the handle type, so you can assign the server directly. Handler = MUX//Create a gorouting specifically to receive this Chan go func () {<-quit If err: = Server. Close (); Err! = Nil {log. Fatal ("Close server:", Err)}} () log. Println ("Start version v3") Err: = Server. Listenandserve () if err! = Nil {if Err = = http. errserverclosed {log. Print ("Server closed under requeset!!") } else { Log. Fatal ("Server closed unexpecteed!!") }} log. Println ("Server exit!!")} Type MyHandler struct{}//self-defined handler structure//Implement MyHandler SERVEHTPP method func (*myhandler) servehttp (w http. Responsewriter, R *http. Request) {w.write ("]byte" ("Hello" is version 3!,the Requeset URL is: "+ r.url. String ()))//The complete URL can be printed here, in response to the root route}func Saybye (w http. Responsewriter, R *http. Request) {time. Sleep (3 * time. Second) W.write ([]byte ("Bye Bye, this is version V3"))//Make a streaming pass, convert the string to a byte type}
- Usage of the base template
Go HTTP Server