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
有疑问加站长微信联系(非本文作者)