Go編程基礎4-Http服務器
阿新 • • 發佈:2018-07-09
mage -h text write ima png pre tex hand 1、http服務器
package main import( "net/http" "log" ) 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("Starting server ... v1") log.Fatal(http.ListenAndServe(":4000",nil)) } func sayBye( w http.ResponseWriter,r *http.Request){ w.Write([]byte("Bye bye,this is version 1!")) } go run test.go 2018/07/09 14:32:08 Starting server ... v1
瀏覽器打開訪問:http://localhost:4000/
瀏覽器打開訪問:http://localhost:4000/bye
Go編程基礎4-Http服務器